Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Shell Flow Control

Shell Flow Control

Unlike languages such as Java and PHP, sh's flow control cannot be empty, e.g. (the following is PHP flow control syntax):

Example

and <. ``` if (( a > b )); then ... fi ``` The following example checks whether two variables are equal: ## Example a = 10 b = 20 if [ $a == $b ] then echo "a equals b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "No matching condition" fi Output: ``` a is less than b ``` Using ((...)) as the test statement: ## Example a = 10 b = 20 if ( ( $a == $b ) ) then echo "a equals b" elif ( ( $a > $b ) ) then echo "a is greater than b" elif ( ( $a < $b ) ) then echo "a is less than b" else echo "No matching condition" fi Output: ``` a is less than b ``` The if else statement is often used together with the test command, as shown below: ## Example num1 =$ [ 2 * 3 ] num2 =$ [ 1 + 5 ] if test $ [ num1 ] -eq $ [ num2 ] then echo 'The two numbers are equal!' else echo 'The two numbers are not equal!' fi Output: ``` The two numbers are equal! ``` --- ## for Loop Similar to other programming languages, Shell supports for loops. The general form of a for loop is: ``` for var in item1 item2 ... itemN do command1 command2 ... commandN done ``` Written on one line: ``` for var in item1 item2 ... itemN; do command1; command2... done; ``` When a variable's value is in the list, the for loop executes all commands once, using the variable name to obtain the current value from the list. Commands can be any valid shell commands and statements. The in list can contain substitutions, strings, and filenames. The in list is optional; if it is not used, the for loop uses the positional parameters of the command line. For example, to output the numbers in the current list in order: ## Example for loop in 1 2 3 4 5 do echo "The value is: $loop" done Output: ``` The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5 ``` To output the characters in a string in order: ``` #!/bin/bash for str in This is a string do echo $str done ``` Output: ``` This is a string ``` --- ## while Statement The while loop is used to continuously execute a series of commands, and also to read data from an input file. Its syntax is: ``` while condition do command done ``` The following is a basic while loop whose test condition is: if int is less than or equal to 5, the condition returns true. int starts at 1, and on each loop iteration int is increased by 1. Running the above script returns the numbers 1 to 5 and then terminates. ## Example #!/bin/bash int = 1 while ( ( $int < = 5 ) ) do echo $int let "int++" done Run the script, output: ``` 1 2 3 4 5 ``` The example above uses the Bash let command, which is used to execute one or more expressions; in variable calculations there is no need to add $ to denote a variable. For details, see: [Bash let Command](/linux/linux-comm-let.html). The while loop can be used to read keyboard input. In the following example, the input is assigned to the variable FILM; press to end the loop. ## Example echo 'Press to exit' echo -n 'Enter your favorite website name: ' while read FILM do echo "Yes! $FILM is a good website" done Run the script, output similar to the following: ``` Press to exit Enter your favorite website name:Runoob Tutorial Yes! Runoob Tutorial is a good website ``` ### Infinite Loops The syntax of an infinite loop: ``` while : do command done ``` or ``` while true do command done ``` or ``` for (( ; ; )) ``` --- ## until Loop The until loop executes a series of commands until the condition becomes true. The until loop is the exact opposite of the while loop in how it works. In general the while loop is preferable to the until loop, but on some occasions - only very rarely - the until loop is more useful. until syntax: ``` until condition do command done ``` condition is usually a conditional expression; if it returns false, the statements in the loop body continue to execute, otherwise the loop is exited. In the following example we use the until command to output the numbers 0 through 9: ## Example #!/bin/bash a = 0 until [ ! $a -lt 10 ] do echo $a a = ` expr $a + 1 ` done Running result: Output result: ``` 0 1 2 3 4 5 6 7 8 9 ``` --- ## case ... esac **case ... esac** is a multi-choice statement, similar to the switch ... case statement in other languages; it is a multi-branch selection structure in which each case branch starts with a right parenthesis and uses two semicolons ;; to indicate break, meaning execution ends and the entire case ... esac statement is exited. esac (case spelled backwards) serves as the end marker. A case statement can be used to match a value against a pattern; if the match succeeds, the corresponding commands are executed. The **case ... esac** syntax is as follows: ``` case value in pattern1) command1 command2 ... commandN ;; pattern2) command1 command2 ... commandN ;; esac ``` case works as shown above: the value must be followed by the word **in**, and each pattern must end with a right parenthesis. The value can be a variable or a constant. Once a match is found between the value and a pattern, all commands in between begin executing until ;;. The value is checked against each pattern in turn. Once a pattern matches, the commands for the matched pattern are executed and no other patterns are considered. If no pattern matches, an asterisk * is used to capture the value, and the following commands are executed. The script below prompts for a number from 1 to 4 and matches it against each pattern: ## Example echo 'Enter a number between 1 and 4:' echo 'The number you entered is:' read aNum case $aNum in 1 ) echo 'You chose 1' ;; 2 ) echo 'You chose 2' ;; 3 ) echo 'You chose 3' ;; 4 ) echo 'You chose 4' ;; * ) echo 'You did not enter a number between 1 and 4' ;; esac Entering different content produces different results, for example: ``` Enter a number between 1 and 4: The number you entered is: 3 You chose 3 ``` The script below matches strings: ## Example #!/bin/sh site = "runoob" case "$site" in "runoob" ) echo "Runoob Tutorial" ;; "google" ) echo "Google Search" ;; "taobao" ) echo "Taobao" ;; esac Output result: ``` Runoob Tutorial ``` --- ## Breaking Out of Loops During a loop, sometimes you need to force an exit before the loop's end condition is reached. Shell uses two commands to do this: **break** and **continue**. ### break Command The break command allows you to exit all loops (terminating all subsequent loops). In the following example, the script enters an infinite loop until the user enters a number greater than 5. To break out of this loop and return to the shell prompt, you need to use the break command. ## Example #!/bin/bash while : do echo -n "Enter a number between 1 and 5:" read aNum case $aNum in 1 | 2 | 3 | 4 | 5 ) echo "The number you entered is $aNum!" ;; * ) echo "The number you entered is not between 1 and 5! Game over" break ;; esac done Run the above code, output result: ``` Enter a number between 1 and 5:3 The number you entered is 3! Enter a number between 1 and 5:7 The number you entered is not between 1 and 5! Game over ``` ### continue The continue command is similar to the break command, with one difference: it does not exit all loops, only the current loop. Modify the example above: ## Example #!/bin/bash while : do echo -n "Enter a number between 1 and 5: " read aNum case $aNum in 1 | 2 | 3 | 4 | 5 ) echo "The number you entered is $aNum!" ;; * ) echo "The number you entered is not between 1 and 5!" continue echo "Game over" ;; esac done Running the code shows that when a number greater than 5 is entered, the loop does not end, and the statement ** echo "Game over"** is never executed.