Shell process control
Unlike Java, PHP and other languages, the process control of sh cannot be empty, such as (the following is how to write PHP process control):
Cannot be written like this in sh/bash, If there is no statement to execute in the else branch, don't write this else.
if else
if
if statement syntax format:
if condition then command1 command2 ... commandN fiWritten in one line (applicable to terminal command prompt):
The fi at the end ofif [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fiis if spelled backwards, and you will encounter similar ones later.
if else
if else Grammar format:
if condition then command1 command2 ... commandN else command fiif else-if else
if else-if else Grammar format:
if condition1 then command1 elif condition2 then command2 else commandN fiThe following example determines whether two variables are equal:
a=10 b=20 if [ $a == $b ] then echo "a 等于 b" elif [ $a -gt $b ] then echo "a 大于 b" elif [ $a -lt $b ] then echo "a 小于 b" else echo "没有符合的条件" fiOutput result:
a 小于 bif else statement is often used in conjunction with the test command, as shown below:
num1=$[2*3] num2=$[1+5] if test $[num1] -eq $[num2] then echo '两个数字相等!' else echo '两个数字不相等!' fiOutput Result:
两个数字相等!
for loop
Similar to other programming languages, Shell supports for loops.
The general format of a for loop is:
for var in item1 item2 ... itemN do command1 command2 ... commandN doneWritten 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 the commands once and uses the variable name to obtain the list the current value of . The command can be any valid shell command and statement. The in list can contain replacements, 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, output the numbers in the current list sequentially:
for loop in 1 2 3 4 5 do echo "The value is: $loop" doneOutput result:
The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5Output the characters in the string sequentially:
for str in 'This is a string' do echo $str doneOutput result :
This is a string
while statement
The while loop is used to continuously execute a series of commands and is also used to read data from the input file; the commands are usually test conditions. Its format is:
while condition do command doneThe following is a basic while loop. The test condition is: if int is less than or equal to 5, then the condition returns true. int starts from 0 and increases by 1 each time the loop is processed. Run the above script, return the numbers 1 to 5, and then terminate.
#!/bin/sh int=1 while(( $int<=5 )) do echo $int let "int++" doneRun the script, output:
1 2 3 4 5The Bash let command is used, which is used to execute one or more expressions. There is no need to add $ to represent variables in variable calculations. For details, please refer to: Bash let command
.The while loop can be used to read keyboard information. In the following example, the input information is set to the variable FILM, and pressing
ends the loop. echo '按下退出' echo -n '输入你最喜欢的电影名: ' while read FILM do echo "是的!$FILM 是一部好电影" done Run the script, the output is similar to the following:
按下退出 输入你最喜欢的电影名: w3cschoolphp中文网 是的!w3cschoolphp中文网 是一部好电影 Infinite loop
Infinite loop syntax format:
while : do command doneor
while true do command doneor
for (( ; ; ))
until loop
until loop executes a series of commands until the condition is true and stops.
The processing methods of until loop and while loop are exactly opposite.
Generally, while loops are better than until loops, but sometimes - and only in rare cases - until loops are more useful.
until Syntax format:
until condition do command doneThe condition can be any test condition. The test occurs at the end of the loop, so the loop is executed at least once - please note this.
case
Shell case statement is a multi-selection statement. You can use case statements to match a value and a pattern. If the match is successful, the matching command is executed. The case statement format is as follows:
case 值 in 模式1) command1 command2 ... commandN ;; 模式2) command1 command2 ... commandN ;; esaccase works as shown above. The value must be followed by the word in, and each pattern must end with a right bracket. The value can be a variable or a constant. After the matching finds that the value matches a certain pattern, all commands start executing until ;;.
The value will detect every matching pattern. Once the pattern is matched, other patterns will not continue after executing the corresponding command of the matching pattern. If none of the patterns match, use the asterisk * to capture the value and execute the following commands.
The following script prompts you to enter 1 to 4 to match each pattern:
echo '输入 1 到 4 之间的数字:' echo '你输入的数字为:' read aNum case $aNum in 1) echo '你选择了 1' ;; 2) echo '你选择了 2' ;; 3) echo '你选择了 3' ;; 4) echo '你选择了 4' ;; *) echo '你没有输入 1 到 4 之间的数字' ;; esacEnter different content, there will be different results, for example:
输入 1 到 4 之间的数字: 你输入的数字为: 3 你选择了 3
Break out of the loop
During the loop process, sometimes it is necessary to force a break out of the loop when the loop end condition is not reached. Shell uses two commands to achieve this function: break and continue.
break command
The break command allows breaking out of all loops (terminating the execution of 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.
#!/bin/bash while : do echo -n "输入 1 到 5 之间的数字:" read aNum case $aNum in 1|2|3|4|5) echo "你输入的数字为 $aNum!" ;; *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束" break ;; esac doneExecute the above code, the output result is:
输入 1 到 5 之间的数字:3 你输入的数字为 3! 输入 1 到 5 之间的数字:7 你输入的数字不是 1 到 5 之间的! 游戏结束continue
The continue command is similar to the break command, with one difference. It will not jump out of all loops, only the current one. cycle.
Modify the above example:
#!/bin/bash while : do echo -n "输入 1 到 5 之间的数字: " read aNum case $aNum in 1|2|3|4|5) echo "你输入的数字为 $aNum!" ;; *) echo "你输入的数字不是 1 到 5 之间的!" continue echo "游戏结束" ;; esac doneRun the code and find that when a number greater than 5 is entered, the loop in this example will not end, and the statementecho "Game is over !"will never be executed.
esac
The syntax of case is very different from the C family language. It requires an esac (the reverse of case) as the end mark, and each case branch uses a right parenthesis. Use two semicolons to indicate break.