Process Control statement:
The IF statement is formatted as follows:
#if语句的后面是Shell命令, if the command performs a successful return of 0, the command following then is executed.
The code is as follows |
Copy Code |
if command Then Command Command Fi
|
#用test命令测试其后面expression的结果, if true, executes the command after then.
The code is as follows |
Copy Code |
if test expression Then Command Fi
|
#下面的格式和test expression equals
The code is as follows |
Copy Code |
If [string/numeric expression] Then Command Fi
|
#下面的两种格式也可以用于判断语句的条件表达式, and they are two of the more commonly used.
The code is as follows |
Copy Code |
if [[string expression]] Then Command Fi
if ((numeric expression)) #let表达式 Then Command Fi
|
See the following example:
The code is as follows |
Copy Code |
/> Cat > test1.sh #从命令行直接编辑test1. sh file. Echo-e "Are You OK" (y/n)? C Read answer
|
#这里的 $answer variable must be enclosed in double quotes, or the judgment will fail. The following echo command is supported when the variable $answer equals y or Y.
The code is as follows |
Copy Code |
If ["$answer" = Y-o "$answer" = y] Then echo "Glad to the It." Fi Ctrl+d />. ./test1.sh Are you OK (y/n)? Y Glad to the IT.
|
The above judgment can also be replaced by:
The code is as follows |
Copy Code |
/> Cat > test2.sh Echo-e "Are You OK" (y/n or Maybe)? C Read answer
|
# [[]] the compound command operator allows the expression to contain the metacharacters, where any word that begins with Y or y is entered, or the maybe performs the following then
The code is as follows |
Copy Code |
Echo if [[$answer = = [yy]* | | $answer = Maybe]] Then echo "Glad to hear it. Fi Ctrl+d />. ./test2.sh Are you OK (y/n or Maybe)? Yes Glad to hear it. |
The following example uses the extended wildcard mode in the shell.
code is as follows |
copy code |
/> shopt-s extglob #打开该扩展模式 /> answer= Not really ' /> If [[$answer = [Nn]o? (way |t really)]] > then &N bsp; > echo "I am sorry." > Fi I am sorry. |
For the extended wildcard character in this example, a specific explanation is needed here. [Nn]o matches no or no,? (Way|t really) represents 0 or 1 (way or t really), so the string that the answer variable matches is no, no, not really, not really, no way, no way.
The following example uses the Let command operator, such as:
The code is as follows |
Copy Code |
/> Cat > test3.sh if (($#!= 2)) #等同于 [$#-ne 2] Then echo "Usage: $ arg1 arg2" 1>&2 Exit 1 #exit退出值为0-255, only 0 indicates success. Fi if (($ < 0 | | >) #等同于 [$1-lt 0-o $1-GT 30] Then echo "Arg1 is out of range." Exit 2 Fi if (($ <=) #等同于 [$2-le 20] Then echo "Arg2 is out of range." Fi Ctrl+d /> Sh./test3.sh Usage:./test3.sh arg1 Arg2 /> echo $? #Shell脚本的退出值为exit的参数值. 1 /> Sh./test3.sh 40 30 Arg1 is out of range. /> echo $? |
2
The following example examines an empty variable in the condition expression of if:
The code is as follows |
Copy Code |
/> Cat > test4.sh If ["$name" = "] #双引号就表示空字符串. Then echo "name is null." Fi Ctrl+d />. ./test4.sh The name is null. |
The use of If/elif/else statements is very similar to if statements, I believe that people with programming experience will not be unfamiliar, here is not to repeat, the format is as follows:
The code is as follows |
Copy Code |
if command Then Command elif command Then Command Else Command Fi
|
See the following sample script:
The code is as follows |
Copy Code |
/> Cat > test5.sh Echo-e "How old are you?" C Read age If [$age-lt 0-o $age-gt] #等同于 (age < 0 | | Age > 120) Then echo "You are the old." elif [$age-ge 0-a $age-le] #等同于 (age >= 0 && Age <= 12)) Then echo "You are child." elif [$age-ge 13-a $age-le] #等同于 (age >= && age <= 19)) Then echo "You are are 13--19 years old." elif [$age-ge 20-a $age-le] #等同于 (age >= && age <= 29)) Then echo "You are are 20--29 years old." elif [$age-ge 30-a $age-le] #等同于 (age >= && Age <= 39)) Then echo "You are are 30--39 years old." Else echo "You are above 40." Fi Ctrl+d />. ./test5.sh How old are you? 50 You are above 40. |
The case statement is formatted as follows:
Case variable in
value1)
Command
;; #相同于C语言中case语句内的break.
value2)
Command
;;
*) #相同于C语言中switch语句内的default
Command
;;
Esac
See the following sample script:
The code is as follows |
Copy Code |
/> Cat > test6.sh #!/bin/sh Echo-n "Choose a color:" Read color Case "$color" in [Bb]l??] echo "you select blue color." ;; [gg]ree*) echo "you select green color." ;; Red|orange) echo "You select Red or orange." ;; *) echo "You select the other color." ;; Esac echo "Out of the case command." />. ./test6.sh Choose a Color:green You select green color. Out of the case command. |