In a while loop, the block of code between the do and done statements is executed so long as the conditional expression is true. Think of it as saying, "Execute while this condition remains true." Here's an example:
while [ "$*" != "" ]
do
echo "Argument value is: $1"
shift
done
This trivial example prints the value of each argument passed to the shell script. Translated to English, the while condition says to continue so long as the input argument string is not null. You could also code the while statement as
while [ -n "$*" ]
but I think the first method is much easier to read and understand.
You might think that this loop would continue to print the first argument ($1) forever and ever, since you don't expect the value of the $* variable (the list of arguments from the command line) to change during the course of running the script. You'd be right, except that I slipped the shift command into the body of the loop.
What shift does is discard the first argument and reassign all the $n variables--so the new $1 variable gets the value that used to be in $2, and so on. Accordingly, the value in $* gets shorter and shorter each time through the loop, and when it finally becomes null, the loop is done.
The until Statement
The until construct works almost exactly the same as while. The only difference is that until executes the body of the loop so long as the conditional expression is false, whereas while executes the body of the loop so long as the conditional expression is true. Think of it as saying, "Execute until this condition becomes true."
Let's code the previous example using an until loop this time and making it a little fancier by adding a counter variable:
count=1
until [ "$*" = "" ]
do
echo "Argument number $count : $1 "
shift
count=`expr $count + 1`
done
Again, you could have coded the until statement as
until [ -z "$*" ]
but I recommend not using the -n and -z operators because it's harder to remember what they do.
The only new concept here is the strange-looking line that increments the counter:
count=`expr $count + 1`
The expr command signals to the shell that we're about to perform a mathematical calculation instead of a string operation. And the doodads that look kind of like single quotation marks are not--they're the backtick (`) character, found to the left of the number 1 key on most keyboards. By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
Note: The spaces on either side of the plus sign are required.
The for Statement
The for statement is yet another way to implement a loop in a shell script. The general form of the for construct is shown here:
for item in list
do
something useful with $item
done
Each time through the loop, the value of the item variable is assigned to the nth item in the list. When you've processed all the items in the list, the loop is done. Here's an example similar to the until and while loops you saw earlier in this section:
for item in "$@"
do
echo "Argument value is: $item"
done
Note that in this example, I used the special variable $@ instead of $* as the list of arguments. The variables $@ and $* both contain the command line arguments, but the for command expects an array (a list of items) as input.
Previous Lesson: Shell Script Logic
Next Lesson: Shell Script Debugging
Comments - most recent first
|
|
||