View and vote on the article here: Shell Programming Series(X)
Shell Programming Series(X)| Category | | | Summary | shift
The shift command is similar to the for loop. Basically, you can use a while loop along with shift to run a loop once for each command-line argument sent to a shell program.
|
| | Body | As you recall from the previous chapters, command-line arguments are stored in numbered variables starting at $1 and going to $9. The shift command shifts the variables one position to the left each time it is run. This means that each time shift is encountered, for example, the information currently stored in $1 ?falls off? the end, and the information currently stored in $2 is moved to $1. Here is an example:
1. #!/bin/sh
2. # Demonstrates the use of shift
3. while [ $# -ne 0 ]
4. do
5. echo ?The value of $1 is now $1.?
6. shift
7. done
8. echo
9. exit 0
And a sample run:
bash$ ./shift1 x y z a c
The value of $1 is now x.
The value of $1 is now y.
The value of $1 is now z.
The value of $1 is now a.
The value of $1 is now c.
bash$
? Line 3: This line starts a while loop. You will recall that the magic variable $# contains the number of command-line arguments. This while loop continues as long as the value of $# is not equal to zero. When the value of $# is equal to zero, all the command-line arguments have been used up, and the loop ends.
? Line 5: This line prints the current value of $1. Note that to actually print the literal string $1 on the screen, you have to escape the $ with the backslash to prevent the shell from giving the $ a special meaning.
? Line 6: When the shift command in line 6 is run, the variables are shifted one position to the left. $1 ?falls off? the end, and is no longer available, $2 becomes $1, $3 becomes $2, and so on.
One of the most common applications of shift (and for loops) is in shell programs that accept filenames as command-line arguments and then perform a group of operations on each file specified on the command line.
The true and false Statements
There are two statements available in shell programming called true and false. The sole purpose of these statements is to return a value of true or false, respectively. These statements can be used to create infinite loops. In the next tutorial, you will learn how to break out of an infinite loop. The following example shows a program that will loop indefinitely.
1. #!/bin/sh
2. # Loops indefinitely.
3. while true
4. do
5. echo ?This line will print forever.?
6. done
7. echo ?This line will never print since the program will?
8. echo ?never get past the loop?
9. exit 0
Clearly, I cannot show you the output of this program since it would go on forever. Line 3 tests for a true condition. And since the argument it test will never return a value of false, the loop will repeat indefinitely because the condition will always be true. Lines 7,8, and 9 will never be executed because the program will never get past the loop.(To end this program, press Ctrl ? C on your keyboard.)
|
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|