This maybe a duplicate of Bash loop control while if else with a return. The method that I am inquiring about might be different. Here's my challenge.
I want my bash script to look for a string in a file. If the string is not found, I want it to say so, wait 10 seconds, and keep looking for the string. Once the string is found, I want it to exit and do other functions. I am having trouble placing the logic of while and if. Code below:
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…fiand thedo other functions herefrom the body of the loop. Theifyou discard. The 'do other functions' code goes after the loop. The loop won't exit until the string is seen; when the string is seen, the other functions will be executed. You may decide you want some sort of timeout on the loop; count the iterations and if the string hasn't appeared inside an hour (360 iterations), give up with an error message and exit.