How Does Shell Script Looping Work?
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 [ "$*" != "" ]
echo "Argument value is: $1"
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:
until [ "$*" = "" ]
echo "Argument number $count : $1 "
count=`expr $count + 1`
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
something useful with $item
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 "$@"
echo "Argument value is: $item"
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.
Comments - most recent first
(Please feel free to answer questions posted by others!)
I am new to Shell Scripting and way behind in my class. Unfortunately, I am in a accelerated 10 wk class and my instructor is working the previous instructors lessons plans. I feel so lost and discouraged. If anyone is available show me how to write a shell script using the below parameters that would be great.
A glass return company requires a program which will calculate the amount of credit due to a customer who returns cases of empty bottles. (One case contains 10 bottles.)
Input to the program is a record containing the customer's name, the number of full cases and the number of bottles in a partial case, if any. If a partial case returned by a customer contains 5 or more bottles, it is to be counted as a full case. Partial cases of less than 5 bottles are not counted.
If 8 or more cases are returned, the customer is credited $4.00 per case, otherwise the customer is credited $3.00 per case.
The program is to print the customer's name, the number of full cases credited, and the credit amount due.
Number of cases
Number of bottles in last case
If number of bottles is grater then or equal to 5, then add 1 to cases
If Total Cases grater then or equal to 8, then multiply cases by 4
else then multiply cases by 3
Total Cases credited
by using this i am geting output like :
now i need to take each line of this output process tht line and remove that line from file after that next line ,
logic can be we count line number such as n=3
use for or while loop on it
tail -1 --it gives last line
take tht line process it
remove tht line from file
this is wht i think but not able to find correct syntax for it in shell script...could anyone help me its very urgent for me...provide me correct syntax
Thanks in Advance...
for item in "$@"
while read LINE
done < yourfile
for ITEM in `cat yourfile`
Note that each item is separated by any whitespace, so multiple items on a line will be treated separately.
for $item in "$*"
it should read:
for item in "$*"
NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.