Выбрать главу

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

You need to end the statements under each condition with a double semicolon (;;). If you do not, the statements under the next condition will also be executed.

The last condition should be default and is executed if none of the other conditions is met. For each of the specified conditions, all the associated statements until breaksw are executed.

The break and exit Statements

You should be aware of two other statements: the break statement and the exit statement.

The break statement can be used to terminate an iteration loop, such as a for, until, or repeat command.

exit statements can be used to exit a shell program. You can optionally use a number after exit. If the current shell program has been called by another shell program, the calling program can check for the code (the $? or $status variable, depending on the shell) and make a decision accordingly.

Using Functions in Shell Scripts

As with other programming languages, shell programs also support functions. A function is a piece of a shell program that performs a particular process; you can reuse the same function multiple times within the shell program. Functions help eliminate the need for duplicating code as you write shell programs.

The following is the format of a function in bash:

func() {

 Statements

}

You can call a function as follows:

func param1 param2 param3

The parameters param1, param2, and so on are optional. You can also pass the parameters as a single string — for example, $@. A function can parse the parameters as if they were positional parameters passed to a shell program from the command line as command-line arguments, but instead use values passed inside the script. For example, the following script uses a function named Displaymonth() that displays the name of the month or an error message if you pass a month number out of the range 1 to 12. This example works with bash:

#!/bin/sh

Displaymonth() {

 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";;