簡體   English   中英

Bash-while循環語法錯誤

[英]Bash - while loop syntax error

我是bash腳本的新手,而atm我正在嘗試學習它。

當我運行此bash代碼時:

#!/bin/bash
while true
do
./pokecli.py
echo ">pokecli exited... restarting...";
sleep 5;
done;    

我收到此錯誤:

opt/PokemonGo-Bot# ./start.sh                         ./start.sh: line 6: syntax error near unexpected token     `done'
./start.sh: line 6: `done;'

任何幫助表示贊賞

您有不需要的分號。 同樣,您應該開始縮進嵌套的命令,並在可讀性上與前一陣子相同。 另外,“ while true”可能很危險,而“ while sleep”則更好。

#!/bin/bash
while sleep 5; do
   ./pokecli.py
   echo ">pokecli exited... restarting..."
done

但是,話雖如此-這些都不會導致錯誤。 您很可能在您的do打上了錯字,將其注釋掉,或者(很可能)在“ do”之前有一個Windows(或其他特殊字符)字符,例如^ M等。 dos2unix命令可能會有所幫助或運行此命令。

tr -cd '[:graph:]\\n\\t ' <start.sh >file.tmp && mv file.tmp start.sh

您將不得不再次chmod +x該腳本。

正確的語法是

7.3樣品時

      #!/bin/bash 
     COUNTER=0
     while [  $COUNTER -lt 10 ]; do
         echo The counter is $COUNTER
         let COUNTER=COUNTER+1 
     done

所以你應該換成

#!/bin/bash
while true; do
  ./pokecli.py
  echo ">pokecli exited... restarting...";
  sleep 5;
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM