0
這可能是Bash loop control while if else with a return的副本。我正在查詢的方法可能會有所不同。這是我的挑戰。while if if bash
我想讓我的bash腳本在文件中尋找一個字符串。如果找不到字符串,我希望它說出來,等待10秒鐘,然後繼續查找字符串。一旦找到字符串,我希望它退出並執行其他功能。我無法放置while和if的邏輯。代碼如下:
while ! grep -q Hello "filename.txt"; do
echo "String Hello not found. Waiting 10 seconds and trying again"
sleep 10
if grep -q Hello "filename.txt"; then
echo "String Hello found in filename.txt. Moving on to next procedure"
sleep 2
return 0
fi
#do other functions here
done
exit
從表面上看,你刪除了'if' ...' fi'和'從循環體中'執行其他函數'。你放棄了'如果'。 '做其他功能'的代碼在循環之後。直到看到字符串,循環纔會退出;當看到字符串時,其他函數將被執行。你可能會決定在循環中想要某種超時;計算迭代次數,如果字符串在一小時內沒有出現(360次迭代),則放棄錯誤消息並退出。 –