2019-03-31 17:05:09 +08:00
.. _cmd-while:
2021-11-06 21:59:53 +08:00
while - perform a set of commands multiple times
================================================
2018-12-17 09:39:33 +08:00
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
2019-09-17 17:59:04 +08:00
::
while CONDITION; COMMANDS...; end
2018-12-18 09:58:24 +08:00
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Description
2019-01-03 12:10:47 +08:00
-----------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` while `` repeatedly executes `` CONDITION `` , and if the exit status is 0, then executes `` COMMANDS `` .
2018-12-17 05:08:41 +08:00
2019-02-25 07:01:16 +08:00
The exit status of the while loop is the exit status of the last iteration of the `` COMMANDS `` executed, or 0 if none were executed. (This matches other shells and is POSIX-compatible.)
2018-12-17 05:08:41 +08:00
2019-03-31 17:24:04 +08:00
You can use :ref: `and <cmd-and>` or :ref: `or <cmd-or>` for complex conditions. Even more complex control can be achieved with `` while true `` containing a :ref: `break <cmd-break>` .
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Example
2019-01-03 12:10:47 +08:00
-------
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
while test -f foo.txt; or test -f bar.txt ; echo file exists; sleep 10; end
2021-02-02 15:35:38 +08:00
# outputs 'file exists' at 10 second intervals,
# as long as the file foo.txt or bar.txt exists.
2018-12-19 11:14:04 +08:00