Logical judgments in shell scripts
If logic is judged. The basic syntax for if judgment in the shell is:
1) without Else
If judgment statement; then COMMANDFI
For example:
[email protected] sbin]# cat if1.sh#! /bin/bashread-p "Please input your score:" AIF (($a <60); Then echo "You didn ' t pass the exam." Fi
In if1.sh (($a <60)) This form, which is a unique format in the shell script, with a parenthesis or no error, please remember this format or can be written as [$a LT 60]. The result of the execution is:
[[Email protected] sbin]# sh if1.shplease input your score:90[[email protected] sbin]# sh if1.shplease input your score: 33You didn ' t pass the exam.
2) with Else
If judgment statement ; then commandelse COMMANDFI
For example:
[email protected] sbin]# cat if2.sh#! /bin/bashread-p "Please input your score:" AIF (($a <60); Then echo "You didn ' t pass the exam." else echo "good! You passed the exam. " Fi
Execution Result:
[Email protected] sbin]# sh if2.shplease input your score:80good! You passed the exam. [Email protected] sbin]# sh if2.shplease input your score:25you didn ' t pass the exam.
The only difference from the previous example is that if you enter a number greater than or equal to 60, you will be prompted.
3) with Elif
If judgment statement one ; then commandelif judgment Statement two; then Commandelse COMMANDFI
For example:
[email protected] sbin]# cat if3.sh#! /bin/bash read-p "Please input your score:" A If (($a <60)); Then echo "You didn ' t pass the exam." Elif (($a >=60) && (($a <85)) and then echo "good! You pass the exam "else echo" very good! Your Socre is very high! "fi
&& here means "and", of course, you can use | | means "or" to execute the result as:
[Email protected] sbin]# sh if3.shplease input your score:90very good! Your Socre is very high! [Email protected] sbin]# sh if3.shplease input your score:60good! You pass the exam.
The above is simply a description of the structure of the IF statement. In determining the size of a value in addition to (()) can be used in the form of [] but can not use;, <, = such a symbol, to use-LT (less than),-GT (greater than),-le (less than equals),-ge (greater than or equal),-eq (equals),-ne (not equals).
[Email protected] sbin]# a=10; If [$a-lt 5]; then echo OK; Fi[[email protected] sbin]# a=10; If [$a-gt 5]; then echo OK; Fiok[[email protected] sbin]# a=10; If [$a-ge 10]; then echo OK; Fiok[[email protected] sbin]# a=10; If [$a-eq 10]; then echo OK; Fiok[[email protected] sbin]# a=10; If [$a-ne 10]; then echo OK; Fi
And look at the If using && | | The situation:
[Email protected] sbin]# a=10; If [$a-lt 1] | | [$a-gt 5]; then echo OK; Fiok[[email protected] sbin]# a=10; If [$a-gt 1] | | [$a-lt 10]; then echo OK; Fiok
In a shell script, if is also often judged about the file attributes, such as whether it is a normal file or a directory, to determine if the files have read and write execution rights. There are several options that are commonly used:
-E: Determine if a file or directory exists
-D: Determine if the directory is not present and whether it exists
-F: Determine if the file is normal and exists
-R: Determine if the document has Read permissions
-W: Determine if Write permission is available
-X: Determine if executable
When you use the If judgment, the specific format is:
if [ -e filename ] ; then
Example:
[Email protected] sbin]# if [-d/home/]; then echo OK; Fiok[[email protected] sbin]# if [-f/home/]; then echo OK; Fi
Because/home/is a non-file directory, "OK" is not displayed.
[Email protected] sbin]# if [-f/root/test.txt]; then echo OK; Fiok[[email protected] sbin]# if [-r/root/test.txt]; then echo OK; Fiok[[email protected] sbin]# if [-w/root/test.txt]; then echo OK; Fiok[[email protected] sbin]# if [-x/root/test.txt]; then echo OK; Fi[[email protected] sbin]# if [-e/root/test1.txt]; then echo OK; Fi
In shell scripts, there is a common way to use if to judge logic, which is case. The specific format is:
Case Variable invalue1) command ;; value2) command ;; VALUE3) command ;; *) command ; Esac
The above structure, which does not limit the number of *
values, represents a value other than value above. The following Amin writes a script that determines whether the input value is odd or even:
[email protected] sbin]# cat case.sh#! /bin/bashread-p "Input a number:" Na=$[$n%2]case $a in 1) echoes "the number is odd." ; 0) echo "The number is even." ;; *) echo "It ' s not a number!" ;; Esac
The value of $a is either 1 or 0, and the result is:
[Email protected] sbin]# sh case.shinput a number:100the number is even. [Email protected] sbin]# sh case.shinput A number:101the number is odd.
Case scripts are often used to write startup scripts for system services, such as those used in/etc/init.d/iptables, so you might want to check them out.
Logical judgments in shell scripts