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 that the condition being tested is just the opposite of the condition specified in the while statement.

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 shift statement 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.

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 if Statement

The if statement evaluates a logical expression to make a decision. An if condition has the following format in 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 isn't necessary for an if condition to have an elif or else part. The else part is executed if none of the expressions that are specified in the if statement and are optional in subsequent elif statements are true. The word fi is used to indicate the end of an if statement, which 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 if statements are properly coded.

In the following example for bash, a variable var can have either of two values: Yes or No. Any other value is invalid. 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

The case Statement

The case statement is used to execute statements depending on a discrete value or a range of values matching the specified variable. In most cases, you can use a case statement instead of an if statement if you have a large number of conditions.

The format of a case statement for bash is as follows:

case str in

 str1 | str2)

  Statements;;

 str3|str4)

  Statements;;

 *)

  Statements;;

esac

You can specify a number of discrete values — such as str1, str2, and so on — for each condition, or you can specify a value with a wildcard. The last condition should be * (asterisk) and is executed if none of the other conditions is met. For each of the specified conditions, all the associated statements until the double semicolon (;;) are executed.

You can write a script that echoes the name of the month if you provide the month number as a parameter. If you provide a number that isn't between 1 and 12, you get an error message. The script is as follows:

#!/bin/sh

case $1 in

 01 | 1) echo 'Month is January';;

 02 | 2) echo 'Month is February';;

 03 | 3) echo 'Month is March';;

 04 | 4) echo 'Month is April';;

 05 | 5) echo 'Month is May';;

 06 | 6) echo 'Month is June';;

 07 | 7) echo 'Month is July';;

 08 | 8) echo 'Month is August';;

 09 | 9) echo 'Month is September';;

 10) echo 'Month is October';;

 11) echo 'Month is November';;

 12) echo 'Month is December';;

 *) echo 'Invalid parameter';;

esac

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату