Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Iteration Statements

Iteration statements are used to repeat a series of commands contained within the iteration statement.

The for Statement

The for statement has a number of formats. The first format is as follows:

for curvar in list
do
    statements
done

This form should be used if you want to execute statements once for each value in list . For each iteration, the current value of the list is assigned to vcurvar. list can be a variable containing a number of items or a list of values separated by spaces. This format of the for statement is used by pdksh and bash.

The second format is as follows:

for curvar
do
    statements
done

In this form, the statements are executed once for each of the positional parameters passed to the shell program. For each iteration, the current value of the positional parameter is assigned to the variable curvar.

This form can also be written as follows:

for curvar in "$@"
do
    statements
done

Remember that $@ gives you a list of positional parameters passed to the shell program, all strung together.

Under tcsh, the for statement is called foreach. The format is as follows:

foreach curvar (list)
    statements
end

In this form, statements are executed once for each value in list and, for each iteration, the current value of list is assigned to curvar.

Suppose you want to create a backup version of each file in a directory to a subdirectory called backup. You can do the following in pdksh and bash:

#!/bin/sh
for filename in `ls`
do
   cp $filename backup/$filename
   if [ $? -ne 0 ]; then
      echo "copy for $filename failed"
   fi
done

In the preceding example, a backup copy of each file is created. If the copy fails, a message is generated.

The same example in tcsh is as follows:

#!/bin/tcsh
foreach filename (`/bin/ls`)
   cp $filename backup/$filename
   if ($? != 0) then
      echo "copy for $filename failed"
   endif
end

The while Statement

The while statement can be used to execute a series of commands while a specified condition is true. The loop terminates as soon as the specified condition evaluates to false. It is possible that the loop will not execute at all if the specified condition evaluates to false right at the beginning. You should be careful with the while command because the loop will never terminate if the specified condition never evaluates to false.

In pdksh and bash, the following format is used:

while expression
do
    statements
done

In tcsh, the following format is used:

while (expression)
    Statements
end

If you want to add the first five even numbers, you can use the following shell program in pdksh and bash:

#!/bin/bash
loopcount=0
result=0
while [ $loopcount -lt 5 ]
do
   loopcount=`expr $loopcount + 1`
   increment=`expr $loopcount \* 2`
   result=`expr $result + $increment`
done

echo "result is $result"

In tcsh, this program can be written as follows:

#!/bin/tcsh
set loopcount = 0
set result = 0

while ($loopcount < 5)
   set loopcount = `expr $loopcount + 1`
   set increment = `expr $loopcount \* 2`
   set result = `expr $result + $increment`

end

echo "result is $result"

The until Statement

The until statement can be used to execute a series of commands until a specified condition is true. The loop terminates as soon as the specified condition evaluates to true.

In pdksh and bash, the following format is used:

until expression
do
    statements
done

As you can see, the format is similar to the while statement.

If you want to add the first five even numbers, you can use the following shell program in pdksh and bash:

#!/bin/bash
loopcount=0
result=0
until [ $loopcount -ge 5 ]
do
   loopcount=`expr $loopcount + 1`
   increment=`expr $loopcount \* 2`
   result=`expr $result + $increment`
done

echo "result is $result"

The example here is identical to the example for the while statement, except the condition being tested is just the opposite of the condition specified in the while statement.

The tcsh command does not support the until statement.

The repeat Statement (tcsh)

The repeat statement is used to execute only one command a fixed number of times.

If you want to print a hyphen (-) 80 times on the screen, you can use the following command:

repeat  80 echo '-'

The select Statement (pdksh)

The select statement is used to generate a menu list if you are writing a shell program that expects input from the user online. The format of the select statement is as follows:

select  item in itemlist
do
    Statements
done

itemlist is optional. If it's not provided, the system iterates through the entries in item one at a time. If itemlist is provided, however, the system iterates for each entry in itemlist and the current value of itemlist is assigned to item for each iteration, which then can be used as part of the statements being executed.

If you want to write a menu that gives the user a choice of picking a Continue or a Finish, you can write the following shell program:

#!/bin/bash
select  item in Continue Finish
do
   if [ $item = "Finish" ]; then
      break
   fi
done

When the select command is executed, the system displays a menu with numeric choices to the user--in this case, 1 for Continue, and 2 for Finish. If the user chooses 1, the variable item contains a value of Continue; if the user chooses 2, the variable item contains a value of Finish. When the user chooses 2, the if statement is executed and the loop terminates.

The shift Statement

The shift statement is used to process the positional parameters, one at a time, from left to right. As you'll remember, the positional parameters are identified as $1, $2, $3, and so on. The effect of the shift command is that each positional parameter is moved one position to the left and the current $1 parameter is lost.

The format of the shift command is as follows:

shift  number

The parameter number is the number of places to be shifted and is optional. If not specified, the default is 1; that is, the parameters are shifted one position to the left. If specified, the parameters are shifted number positions to the left.

The shift command is useful when you are writing shell programs in which a user can pass various options. Depending on the specified option, the parameters that follow can mean different things or might not be there at all.

Share ThisShare This

Informit Network