I. Logical judgments in shell scripts
Grammar
1. Format 1
if condition; Then statement; Fi
Example: If a is greater than 3, print OK
[[email protected] ~]# vi 2.sh[[email protected] ~]# bash -v 2.sh #!/bin/basha=5if [ $a -gt 3 ]then
2. Format 2
if condition; Then statement; else statement; Fi
Example: If a is less than 3, print no OK
[[email protected] ~]# bash -v 3.sh #!/bin/basha=2if [ $a -gt 3 ]then echo okelse
3. Format 3
If ...; Then ...; Elif ...; Then ...; else ...; Fi
Cases:
[[email protected] ~]# sh -x 4.sh+ a=3+ ‘[‘ 3 -gt 4 ‘]‘+ ‘[‘ 3 -lt 6 ‘]‘+ echo ‘<6 && >1‘<6 && >1[[email protected] ~]# sh -v 4.sh#!/bin/basha=3if [ $a -gt 4 ]then echo ">1"elif [ $a -lt 6 ]then echo "<6 && >1"else
Logical Judgment Expression
If [$a-gt $b]; If [$a-lt 5]; If [$b-eq 10] etc.
-GT (>); Greater than
-lt (<); Less than
-ge (>=); Greater than or equal
-le (<=); Less than or equal to
-eq (= =) equals
; -ne (! =)
Note that there are spaces everywhere.
can use && | | Combine multiple conditions
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then
Second, the file directory attribute judgment
1. If judgment file, directory attribute
[-F file] Determines if it is a normal file, and there is
Example: If the riven file does not exist, create
#!/bin/bashc="/tmp/riven"if [ -f $c ]then echo $c existelse touch $cfi
[-D file] Determines if it is a directory and exists
#!/bin/bashc="/tmp/2018"if [ -d $c ]then echo $c existelse touch $cfi
[-E file] to determine whether files or directories exist
#!/bin/bashc="/tmp/2018"if [ -e $c ]then echo $c existelse touch $cfi
[-R File] to determine if the document is readable
#!/bin/bashc="/tmp/2018"if [ -r $c ]then echo $c readableelse touch $cfi
[-W file] Determines whether the file is writable
#!/bin/bashc="/tmp/2018"if [ -w $c ]then echo $c writeableelse touch $cfi
[-X file] Determines whether the file is executable
#!/bin/bashc="/tmp/2018"if [ -x $c ]then echo $c exeableelse touch $cfi
Because the file is not executable, all have no output.
2. Script abbreviation
#!/bin/bashc="/tmp/2018"[ -f $f ] && rm -f $f#上面的命令等同于下面的命令if [-f $f ]then rm -f $ffi
#!/bin/bashc="/tmp/2018"[ -f $f ] || touch $f #如果这个文件不存在就执行后面的文件#上面的命令等同于下面的命令if [ !-f $f ] #叹号表示取反then touch $ffi
Third, if special usage
1,if [-Z "$a"]?? This indicates that an error occurs when the value of variable A is empty
#!/bin/bashif [ ! -f /tmp/a1 ]then echo "/tmp/a1 not exist." exit n=`wc -l /tmp/a1`if [ -z "$n" ]then echo error exitelse if [ $n -gt 100 ]then echo okfi
If [-n ' $a '] means that the value of variable A is not empty
If Grep-q ' 123 ' 1.txt; Then?? What happens if the 1.txt contains a ' 123 ' row?
[[email protected] ~]# grep -w ‘root‘ /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin[[email protected] ~]# if grep -w ‘root‘ /etc/passwd; then echo "root exist"; firoot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinroot exist[[email protected] ~]# if grep -wq ‘root‘ /etc/passwd; then echo "root exist"; fi #-q不显示过滤内容root exist[[email protected] ~]# if ! grep -w ‘user1‘ /etc/passwd;then useradd user1;fi # !取反
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []
Iv. Case Judgment
Case judgment in the 1?shell
Format:
case??量名 in value1) command ;; value2) command ;; *) commond ;; esac
In a case program, you can use the |, meaning, or mean of a condition such as
2|3)
Command
;;
2. Script case
#!/bin/bashread -p "Please input a number: " n #read命令让用户输入字符串,n表示要捕获的变量。if [ -z "$n" ] then echo "Please input a number." exit 1 fin1=`echo $n|sed ‘s/[0-9]//g‘` #if [ -n "$n1" ]then echo "Please input a number." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The number range is 0-100." ;; esac
Logical judgments in shell scripts, file directory attribute judgments, if special usages, case judgments