The conditional judgment statement in the shell is an upgrade to the previous "conditional test statement in the shell", meaning that the preceding test statement serves the IF~THEN~FI statement for the current judgment statement.
We're still rolling it out in the form of attention points and code implementations:
1) The basic IF-THEN-FI statement can be used to judge the basic single-layer branching structure in the following form:
The test statement after if is generally done using the [] command. As in the following example:
#-----------------------------/chapter4/ex4-18.sh------------------#! /bin/sh# use condition test to determine if/bin/bash is a regular file if [-f/bin/bash]; Then echo "/bin/bash is a file" fi
2) The IF-THEN-ELSE-FI statement can handle two-level branch judgment statements. As follows:
#-----------------------------/chapter4/ex4-22.sh------------------#! /bin/sh# output Prompt message echo "Please enter a number:" #从键盘读取用户输入的数字read num# if the user enters more than 10if ["$num"-GT 10]; then# output greater than 10 the message echo "the number is greater than 10." #否则else # output less than or equal to 10 of the message echo "The number is equal to or lesser than 10." Fi
3) If-then-elif-then-elif-then-...-Else-fi. This statement can achieve multiple judgments, and note that you must end with an else . As follows:
#-----------------------------/chapter4/ex4-24.sh------------------#! /bin/shecho "Please enter a score:" Read Scoreif [-Z "$score"]; Then echo, "you enter nothing." Please enter a score: " read Scoreelse if [" $score "-lt 0-o" $score "-GT 100]; Then echo "The score should is between 0 and 100.Please enter again:" read score else #如果成绩大于90 if [" $score "-ge 90]; Then Echo, "The grade is A." #如果成绩大于80且小于90 elif ["$score"-ge]; then echo "The grade is B." #如果成绩大于70且小于80 elif ["$score"-ge]; then echo "The grade is C." #如果成绩大于60且小于70 elif ["$score"-ge]; then echo "The grade is D." #如果成绩小于60 Else echo "The grade is E." Fi Fifi
4) to exit the program, you can use the Exit Status statement. An exit status of 0 means that the script runs at the end of the run, and 0 indicates that there are some errors in the execution of the program, and that different errors correspond to different exit states. Although the user can customize the exit state status in the program, usually each status has a specific meaning, so be sure to avoid ambiguity when customizing to return to status. Examples are as follows:
#-----------------------------/chapter4/ex4-26.sh------------------ #!/bin/sh03 If the file already exists, exit directly, and the first parameter entered in the file name is if [-e] "]06 then07 echo" file $ exists. Exit 109 #如果文件不存在, create the file, use touch to create the file, or use the redirect to create the file echo "hello~" >./test.log That is, create a new Test.log file in the current directory . ELSE11 Touch "$" " file $ been created." Exit 014 fi
5) CASE-ESAC statement to achieve multiple conditional judgment. As follows:
Note: At the end of each variable's content, you need two semicolons (;;) to represent the end of the paragraph of the program, the end of each variable case), the remainder is represented by the *), and finally the ESAC to the end, that is, the cases.
#-----------------------------/chapter4/ex4-27.sh------------------#! /bin/sh# output Cue message echo "hit a key,then hit return." #读取用户按下的键read Keypress#case statement begins case "$keypress" in #小写字母 [[: Lower:]]) echo "lowercase letter.";; #大写字母 [[: Upper:]] echo "uppercase letter.";; #单个数字 [0-9]) echo "Digit.";; #其他字符 *) echo "other letter.";; Esac
Reference:
"Brother Bird's Linux private dish"
"Shell from Beginner to mastery"
Conditional judgment statements in the shell If~then~fi