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:
while [ condition ]
do
code block;
done
do
code block;
done
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.
verify="n"
while [ "$verify" != y ]
do
echo "Enter option: "
read option
echo "You entered $option. Is this correct? (y/n)"
read verify
done
while [ "$verify" != y ]
do
echo "Enter option: "
read option
echo "You entered $option. Is this correct? (y/n)"
read verify
done
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):
while [ 1 ]
do
ps -ef | grep [s]endmail | wc -l
sleep 5
done
do
ps -ef | grep [s]endmail | wc -l
sleep 5
done
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 [s]endmail are a regular expression trick that prevents the 'grep [s]endmail' process from being counted.
Also see ...
Bourne/bash shell script functions
H3Writing functions can greatly simplify a program. If a chunk of code is used multiple times in different parts of a script, the code can be enclosed within a function and run using only the function name./H3PThe following example decribes the use of functions in a Bourne shell script. br /
H3Writing functions can greatly simplify a program. If a chunk of code is used multiple times in different parts of a script, the code can be enclosed within a function and run using only the function name./H3PThe following example decribes the use of functions in a Bourne shell script. br /
bash shell script declaring/creating arrays
H3The use of array variable structures can be invaluable. This recipe describes several methods for delcaring arrays in bash scripts./H3PThe following are methods for declaring arrays: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie &41;P br /This creates an ar
H3The use of array variable structures can be invaluable. This recipe describes several methods for delcaring arrays in bash scripts./H3PThe following are methods for declaring arrays: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie &41;P br /This creates an ar
bash shell script iterate through array values
H3Having an array of variables is of no use unless you can use those values somehow. This recipe shows a few methods for looping through the values of an array in the bash shell./H3PGiven the array definition: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie &41;P
H3Having an array of variables is of no use unless you can use those values somehow. This recipe shows a few methods for looping through the values of an array in the bash shell./H3PGiven the array definition: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie &41;P
bash shell script accessing array variables
H3The bash shell allows a number of methods for accessing elements of variable arrays. This recipe demonstrates some of these techniques./H3PGiven the array defined by the following code: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie Molly Millie&41;P br /The
H3The bash shell allows a number of methods for accessing elements of variable arrays. This recipe demonstrates some of these techniques./H3PGiven the array defined by the following code: br / br /div class="code"names=&40; Jennifer Tonya Anna Sadie Molly Millie&41;P br /The
bash array operations
H3sometimes we need to remove one element from an array for example we remove the element 2 (third) from array/H3Pspan style="color: green" br /array=("hello" "i" "like" "bash") br /i=2 br /array=(${array[@]:0:$i} ${array[@]:$(($i + 1))}) br //span/P
H3sometimes we need to remove one element from an array for example we remove the element 2 (third) from array/H3Pspan style="color: green" br /array=("hello" "i" "like" "bash") br /i=2 br /array=(${array[@]:0:$i} ${array[@]:$(($i + 1))}) br //span/P
