Title: Bourne/bash shell script: while loop syntax Post by: Mark David on March 16, 2007, 01:22:47 PM Bourne/bash shell script: while loop syntax
A while loop allows execution of a code block an arbitrary number of times until a condition is met. This recipe describes the while loop syntax for the various Bourne shells (sh, ksh, bash, zsh, etc.) and provides examples. General syntax: Code: while [ condition ] Any valid conditional expression will work in the while loop. The following code asks the user for input and verifies the input asking for a y or n and will repeat the process until a y answer is received. Code: verify="n" Another useful while loop is a simple one, and infinite loop. If the conditional is "1" the loop will run until something else breaks it (such as the break keyword): Code: while [ 1 ] This while loop will display the number of sendmail processes running every five seconds until the loop or executing shell is killed. The brackets in |