Command Format: for loop-index in argument-list
do
commands
done
This structure will assign the value of the first item in the
argument list to the loop index and executes the commands between
the do and done statements. The do and done statements indicate
the beginning and end of the for loop.
After the structure passes control to the done statement, it
assigns the value of the second item in the argument list to the
loop index and repeats the commands. The structure will repeat
the commands between the do and done statements once for each
argument in the argument list. When the argument list has been
exhausted, control passes to the statement following the done.
Sample Session:
$cat find_henry1
for x in project1 project2 project3
do
grep henry $x
done
Sample Session:
$head project?
==> project1 <==
henry
joe
mike
sue
==> project2 <==
joe
mike
sue
==> project3 <==
joe
mike
sue
henry
==> project4 <==
joe
mike
$find_henry
henry
henry
$
Each file in the argument list was searched for the string, henry.
When a match was found, the string was printed.
As long as the expression returns a true exit status, the structure
continues to execute the commands between the do and the done
statement. Before each loop through the commands, the structure
executes the expression. When the exit status of the expression
is false (non-zero), control is passed to the statement following
the done statement.
The commands to be executed must change the expression test or an
infinite loop can result.
The until and while structures are very similar. The only
difference is that the test is at the top of the loop. The until
structure will continue to loop until the expression returns true
or a nonerror condition. The while loop will continue as long as
a true or nonerror condition is returned.
The commands to be executed must change the expression test or an
infinite loop can result.
Sample Session:
$cat until_ex
secretname='jenny'
name='noname'
echo 'Try to guess the secret name!'
echo
until (test "$name" = "$secretname")
do
echo 'Your guess: \c'
read name
done
echo 'You did it!'
$
The until loop will continue until name is equal to the secret
name.
Sample Session:
$chmod a+x until_ex
$until_ex
Try to guess the secret name!
Your guess: gaylan
Your guess: art
Your guess: richard
Your guess: jenny
You did it!
$
The case structure allows a multiple-branch decision mechanism.
The path that is taken depends on a match between the test-string
and one of the patterns.
Sample Session:
$cat case_ex
echo 'Enter A, B, or C: \c'
read letter
case $letter in
A) echo 'You entered A' ;;
B) echo 'You entered B' ;;
C) echo 'You entered C' ;;
*) echo 'You did not enter A, B, or C' ;;
esac
$chmod a+x case_ex
$case_ex
Enter A, B, or C: B
You entered B
$case_ex
Enter A, B, or C: b
You did not enter A, B, or C
$
This example uses the value of a character that the user entered
as the test string. The value is represented by the variable
letter. If letter has the value of A, the structure will execute
the command following A. If letter has a value of B or C, then
the appropriate commands will be executed. The asterisk indicates
any string of characters; and it, therefore, functions as a
catchall for a no-match condition. The lowercase b in the second
sample session is an example of a no match condition.