English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Unlike languages such as Java and PHP, sh's flow control cannot be empty, as shown in the following PHP flow control (writing):
<?php if (isset($_GET["q"])) { search(q); } else { // do nothing }
In sh/In bash, you cannot write it like this; if there is no statement to be executed in the else branch, do not write this else.
The if statement syntax format is:
if condition then command1 command2 ... commandN fi
written on one line (for terminal command prompt):
if [ $(ps -ef | grep -c "ssh" -gt 1 ]; then echo "true"; fi
The final fi is the reverse spelling of if, and you will encounter similar ones later.
The if else syntax format is:
if condition then command1 command2 ... commandN else command fi
if else-The if else syntax format is:
if condition1 then command1 elif condition2 then command2 else commandN fi
The following example demonstrates how to determine if two variables are equal:
a=10 b=20 if [ $a == $b ] then echo "a is equal to 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 conditions" fi
Output result:
a is less than b
if else statements are often used in conjunction with the test command, as shown below:
num1=$[2*3] num2=$[1+5] if test $[num1] -eq $[num2] then echo 'Two numbers are equal!' else echo 'Two numbers are not equal!' fi
Output result:
Two numbers are equal!
Similar to other programming languages, Shell supports for loops.
The general format of the for loop is:
for var in item1 item2 ... itemN do command1 command2 ... commandN done
Written in one line:
for var in item1 item2 ... itemN; do command1; command2... done;
When the variable value is in the list, the for loop executes all commands once, using the variable name to get the current value from the list. Commands can be any valid shell command and statement. The 'in' list can contain substitutions, strings, and filenames.
The 'in' list is optional. If it is not used, the for loop uses the command line's position arguments.
For example, sequentially output the numbers in the current list:
for loop in 1 2 3 4 5 do echo "The value is: $loop" done
Output result:
The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5
Sequentially output the characters in the string:
#!/bin/bash for str in This is a string do echo $str done
Output result:
This is a string
The while loop is used to repeatedly execute a series of commands and is also used to read data from an input file. Its syntax format is:
while condition do command done
The following is a basic while loop, the test condition is: if int is less than or equal to 5so the condition returns true. int starts from 1 starts, and int is incremented each time the loop is processed. 1. Run the above script, and the number 1 to 5and then terminate.
#!/bin/bash int=1 while(( $int<=5 )) do echo $int let "int++" done
Run the script, and the output will be:
1 2 3 4 5
The above example uses the Bash let command, which is used to execute one or more expressions, and variables do not need to be prefixed with $ for variable calculation. For more information, please refer to:Bash let command
.
The while loop can be used to read keyboard information. In the following example, input information is set to the variable FILM, press <Ctrl-D> End loop.
echo 'Press <CTRL-D> Exit' echo -n 'Enter the name of your favorite website: ' while read FILM do echo "Yes! $FILM is a good website" done
Run the script, and the output will be similar to the following:
Press <CTRL-D> Exit Enter the name of your favorite website: Basic Tutorial Website Yes! Basic Tutorial Website is a good website
Infinite loop syntax format:
while : do command done
or
while true do command done
or
for (( ; ; ))
The until loop executes a series of commands until the condition becomes true and then stops.
The until loop is exactly the opposite in handling compared to the while loop.
Generally, the while loop is better than the until loop, but in some cases—only in a few cases—the until loop is more useful.
until syntax format:
until condition do command done
The condition is generally a conditional expression. If the return value is false, the statements within the loop body are continued to be executed, otherwise the loop is exited.
In the following example, we use the until command to output 0 ~ 9 Enter the number:
#!/bin/bash a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done
Run results:
The output is:
0 1 2 3 4 5 6 7 8 9
case ... esac It is a multi-choice statement, similar to the switch ... case statement in other languages, and is a multi-branch selection structure. Each case branch starts with a right parenthesis, and is terminated by two semicolons ;;, which means the execution is finished and the entire case ... esac statement is exited. esac (which is the reverse of case) is used as the end mark.
The case statement can be used to match a value with a pattern. If a match is successful, the corresponding command is executed.
case ... esac Syntax format is as follows:
case "value" in pattern1) command1 command2 ... commandN ;; pattern2) command1 command2 ... commandN ;; esac
case operation as shown above, the value must be followed by the word inis used, and each pattern must end with a right parenthesis. The value can be a variable or a constant, and once a match is found, all commands between it will start executing until ;;.
The value will check each pattern for a match. Once a pattern matches, the corresponding command after the match is executed and the other patterns are no longer continued. If no matching pattern is found, the asterisk * Capture this value and then execute the following command.
The following script prompts for input 1 to 4,matching each pattern:
echo 'Enter' 1 to 4 between the numbers:' echo 'The number you entered is:' read aNum case $aNum in 1) echo 'You have selected' 1' ;; 2) echo 'You have selected' 2' ;; 3) echo 'You have selected' 3' ;; 4) echo 'You have selected' 4' ;; *) echo 'You did not enter' 1 to 4 between the numbers' ;; esac
Different inputs will result in different outputs, for example:
Input 1 to 4 numbers between: The number you entered is: 3 You have selected 3
The following script matches strings:
#!/bin/sh site="w3codebox" case "$site" in "w3codebox) echo "Basic Tutorial Website" ;; "google") echo "Google Search" ;; "taobao") echo "Taobao website" ;; esac
The output is:
Basic Tutorial Website
Sometimes, when the loop ending condition has not been reached, it is necessary to force an exit from the loop. Shell uses two commands to achieve this function: break and continue.
The break command allows you to jump out of all loops (terminate the execution of all loops behind it).
In the following example, the script enters an infinite loop until the user enters a number greater than5. To exit this loop and return to the shell prompt, you need to use the break command.
#!/bin/bash while : do echo -n "Input 1 to 5 numbers between: read aNum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ;; *) echo "The number you entered is not 1 to 5 between the ! Game Over" break ;; esac done
After executing the above code, the output is:
Input 1 to 5 numbers between:3 The number you entered is 3! Input 1 to 5 numbers between:7 The number you entered is not 1 to 5 between the ! Game Over
The continue command is similar to the break command, but there is one difference, it does not jump out of all loops, but only out of the current loop.
Modify the above example:
#!/bin/bash while : do echo -n "Input 1 to 5 numbers between: read aNum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ;; *) echo "The number you entered is not 1 to 5 between the !" continue echo "Game Over" ;; esac done
When the input is greater than5When the number is entered, the loop in this example will not end, the statementecho "Game Over"Will Never Be Executed.