View and vote on the article here: Shell Programming Series (VII)
Shell Programming Series (VII)| Category | | | Summary | | Sometimes you might want an operation to repeat until a certain condition becomes true (or until a certain condition is no longer true). This is where looping statements come into play. The Bourne shell supports three different looping constructs for thes |
| | Body | The while Loop
The while loop repeats the statements inside the loop as long as the condition that is being tested is true. Depending on your point of view, it could also be said that the while loop repeats the statements inside the loop until the condition that is being tested becomes false. If the condition is already false the first time it is evaluated, the loop will never be executed. For example, the following program uses a while loop to count to 20 and display the numbers on the screen.
1. #!/bin/sh
2. # Count from 1 to 20
3. i=1
4. while [ $i -le 20 ]
5. do
6. echo $i
7. i=`expr $i + 1`
8. done
9. exit 0
This program introduces a few new concepts:
? Line 3: This line simply assigns the variable I the initial value of 1. I is a variable that is universally understood to be a loop control counter, so this is one case where you can get away with not using a descriptive variable name.
? Line 4: The while command contains the condition to test enclosed in brackets. The bracket is actually a shorthand notation for a command called test. You will use this command a lot in shell scripting.
Unfortunately, the test command uses a somewhat strange syntax. -le in this example means ?is less than or equal to?. So this means the loop will repeat as long as the variable ?i? is less than or equal to 20. For mathematical evaluations, following shows all the operators that this command supports:
<TT>-eq</TT> --> True if operand one is equal to operand two
<TT>-ne</TT> --> True if operand one is not equal to operand two
<TT>-gt</TT> --> True if operand one is greater than operand two
<TT>-ge</TT> --> True if operand one is greater than or equal to operand two
<TT>-lt</TT> --> True if operand one is less than operand two
<TT>-le</TT> --> True if operand one is less than or equal to operand two
? Line 5: The do statement indicates that everything after this should be done for each iteration of the loop. All statements located between the do statement and the done statement are considered part of the loop.
? Line 6: Prints the current value of the variable i.
? Line 7: Uses command substitution with the expr command to increment the value of i by one and then assign the new value back into i.
? Line 8: Indicates the end of the loop. At this point, the program jumps back up to the while statement, and the test condition is evaluated again. If i is still less than or equal to 20, the statements inside the loop are repeated again. If i is greater than 20, the loop exits and control passes to the first statement after the done statement.
Notice that the statements inside the loop are indented. This makes it easy to pick out the statements that are part of the loop when quickly scanning the program's source code. The shell ignores the indentation, so you could have written this program without using it. I recommend you always indent loops, though, to make them easier to find when quickly scanning program source code.
The space between the [ and the condition to be tested is compulsory. falling to include it will cause an error. For example, [ $var1 -gt 5 ] will work, but [$var1 -gt 5] will cause an error.
|
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|