If you want the while loop to stop after some condition, and your
foo command returns non-zero when this condition is met then you can get the loop to break like this:
while foo; do echo 'sleeping...'; sleep 5; done;
For example, if the
foo command is deleting things in batches, and it returns 1 when there is nothing left to delete.
This works well if you have a custom script that needs to run a command many times until some condition. You write the script to exit with
1 when the condition is met and exit with
0 when it should be run again.
For example, say you have a python script
batch_update.py which updates 100 rows in a database and returns
0 if there are more to update and
1 if there are no more. The the following command will allow you to update rows 100 at a time with sleeping for 5 seconds between updates:
while batch_update.py; do echo 'sleeping...'; sleep 5; done;