This article is also the fifth chapter of "Learning the bash Shell" 3rd Edition, five of Flow Control's reading notes, but we will not limit it to this. Flow control is a common part of any programming language, including while and util, which are also common in advanced programming languages. Here, we will continue to learn about them.
The while and util syntaxes are the same, as follows:
While
|Util
Condition;
Do
Statements
...
Done
Like if, condition may consist of a series of statements whose value is the exit status of the final statment. The while and until conditions are just the same. The while check conditions are executed when they are true, while the until conditions are false. Unlike C, util sticks to the condition from the beginning, rather than at the end of each loop. If you add "! ", While and util are actually no different, but until is a bit interesting in the following situation:
Until
Command
;Do
Statements
...
Done
Execute statements until the command runs correctly.
Until cp $1 $2; do
Echo 'attempt to copy failed. waiting ...'
Sleep 5
Done
For example, in the above example, try the copy command every five seconds until the copy is successful. You can also use the while! Cp $1 $2; do... method.
Add the bash shell script with the suffix sh, so that different colors can be displayed in gedit compilation, which is more convenient.
Related links:
My articles on Linux operations