-->

Previous | Table of Contents | Next

Page 427

This form can also be written as follows:


for curvar in "$@"

do

    statements

done

Remember that $@ provides you a list of positional parameters passed to the shell program, all stringed 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 the directory to a subdirectory called backup. You can do the following in pdksh and bash:


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, and if the copy fails, a message is generated.

The same example in tcsh is as follows:


foreach filename (`ls`)

   cp $filename backup/$filename

   if $? _ne 0 then

      echo "copy for $filename failed"

   fi

end

The while Statement

The while statement can be used to execute a series of commands while a specified condition is true. The loop will terminate 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, as the loop might never terminate if the specified condition never evaluates to false.

In pdksh and bash, the following format is used:


while expression

do

    statements

done

Page 428

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:


loopcount=0

result=0

while [ $loopcount -lt 5 ]

do

   loopcount = `expr $loopcount + 1`

   result = `$result + ($loopcount * 2)`

done



echo "result is $result"

In tcsh, this program can be written as follows:


set loopcount = 0

set result = 0

while ( $loopcount < 5 )

   set loopcount  =  `expr $loopcount + 1`

   set result  =  `$result + ($loopcount * 2)`

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 will terminate 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:


loopcount=0

result=0

until [ $loopcount -ge 5 ]

do

   loopcount = `expr $loopcount + 1`

   result = `$result + ($loopcount * 2)`

done



echo "result is $result"

Page 429

The example here is identical to the example for the while statement, except that 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 select statement is as follows:


select  item in itemlist

do

    Statements

done

itemlist is optional. If not provided, then the system will iterate through the entries in item one at a time. If, however, itemlist is provided, then the system will iterate 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, then you can write the following shell program:


select  item in Continue Finish

do

   if [ $item = "Finish" ] then

      break

   fi

done

When the select command is executed, the system will display 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 will contain a value of Continue, and if the user chooses 2, the variable item will contain a value of Finish. When 2 is chosen by the user, the if statement will be executed and the loop will terminate.

The shift Statement

The shift statement is used to process the positional parameters, one at a time, from left to right. As you 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.

Page 430

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, then 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 different options. Depending on the specified option, the parameters that follow can mean different things or might not be there at all.

Conditional Statements

Conditional statements are used in shell programs to decide which part of the program to execute depending on specified conditions.

The if Statement

The if statement evaluates a logical expression to make a decision. An if condition has the following format in pdksh and bash:


if [ expression ] then

    Statements

elif [expression ] then

    Statements

else

    Statements

fi

The if conditions can be nested. That is, an if condition can contain another if condition within it. It is not necessary for an if condition to have an elif or else part. The else part is executed if none of the expressions specified in the if statement and optional in subsequent elif statements are true. The word fi is used to indicate the end of the if statements. This is very useful if you have nested if conditions. In such a case you should be able to match fi to if to ensure that all the if statements are properly coded.

In the following example, a variable var can have two values: Yes and No. Any other value is an invalid value. This can be coded as follows:


if [ $var = "Yes" ] then

   echo "Value is Yes"

elif [ $var = "No" ] then

   echo "Value is No"

else

   echo "Invalid value"

fi

In tcsh, the if statement has two forms. The first form, similar to the one for pdksh and bash, is as follows:

Previous | Table of Contents | Next