Logical judgments in shell scripts
1.if then fi
[[email protected] shell]# cat if1.sh#!/bin/basha=5if [ $a -gt 3 ]then echo okfi
2.if then else fi:
[email protected] shell]# sh -x if2.sh + a=1+ ‘[‘ 1 -gt 3 ‘]‘+ echo nooknook[[email protected] shell]# cat if2.sh #!/bin/basha=1if [ $a -gt 3 ]then echo okelse echo nookfi
3.if then elif then else fi:
[[email protected] shell]# cat if3.sh #!/bin/basha=4if [ $a -gt 4 ]then echo ">1"elif [ $a -lt 4 ]then echo "<4"else echo "=4"fi
[[email protected] shell]# sh -x if3.sh + a=3+ ‘[‘ 3 -gt 4 ‘]‘+ ‘[‘ 3 -lt 4 ‘]‘+ echo ‘<4‘<4[[email protected] shell]# vi if3.sh [[email protected] shell]# sh -x if3.sh + a=4+ ‘[‘ 4 -gt 4 ‘]‘+ ‘[‘ 4 -lt 4 ‘]‘+ echo =4=4[[email protected] shell]# cat if3.sh #!/bin/basha=4
4. Note "" Both sides need a space,-gt greater than-lt less than-eq equals both sides need a space-ge greater than or equal to-le less than equals noeq not equal to
5.if Logic Judgment Support | | and &&
File Directory property judgment
1.-f file to determine whether it is a normal document, and there are
[[email protected] shell]# sh -x file1.sh + f=/tmp/aminglinux+ ‘[‘ -f /tmp/aminglinux ‘]‘+ touch /tmp/aminglinux[[email protected] shell]# cat file1.sh #!/bin/bashf="/tmp/aminglinux"if [ -f $f ]then echo $f existelse touch $ffi[[email protected] shell]# sh -x file1.sh + f=/tmp/aminglinux+ ‘[‘ -f /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux exist/tmp/aminglinux exist
2.-d file Determines whether it is a directory and exists:
[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -d /tmp/aminglinux ‘]‘+ touch /tmp/aminglinux[[email protected] shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -d $f ]then echo $d existelse touch $ffi
3.-E determine whether a file or directory exists:
[[email protected] shell]# vi file2.sh [[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -e /tmp/aminglinux ‘]‘+ echo existexist
4.-r to determine if a file is readable
[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -r /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux readable/tmp/aminglinux readable[[email protected] shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -r $f ]then echo $f readablefi
5.-w to determine if a file is writable
[[email protected] shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -w $f ]then echo $f writeablefi[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -w /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux writeable/tmp/aminglinux writeable
6.-x determine if executable: unenforceable, no output
[[email protected] shell]# ls -l /tmp/aminglinux -rw-r--r-- 1 root root 0 4月 18 21:36 /tmp/aminglinux[[email protected] shell]# vi file2.sh [[email protected] shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -x $f ]then echo $f exeablefi[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -x /tmp/aminglinux ‘]‘
If special usage
1. Determine if the variable is empty:
++ wc -l /tmp/lalalwc: /tmp/lalal: 没有那个文件或目录+ n=+ ‘[‘ -gt 100 ‘]‘if4.sh: 第 3 行:[: -gt: 期待一元表达式[[email protected] shell]# vi if4.sh[[email protected] shell]# sh -x if4.sh ++ wc -l /tmp/lalalwc: /tmp/lalal: 没有那个文件或目录+ n=+ ‘[‘ -z ‘‘ ‘]‘+ echo errorerror[[email protected] shell]# cat if4.sh #!/bin/bashn=`wc -l /tmp/lalal`if [ -z "$n" ]then echo errorelif [ $n -gt 100 ]then echo aldkjglkafi
2.-n to determine if it is not empty:
[[email protected] shell]# ls01.sh file1.sh file2.sh if1.sh if2.sh if3.sh if4.sh[[email protected] shell]# if [ -n 01.sh ]; then echo ok; fiok
What happens when the 3.-q file contains characters:
[[email protected] shell]# if grep -wq ‘weixing01‘ /etc/passwd; then echo "sdjfk"; fisdjfk
Case Judgment
1. Scripting:
[[email protected] shell]# cat case1.sh #!/bin/bashread-p "Please input a number:" Nif [-Z "$n"]then echo "Please input a number." Exit 1fin1= ' echo $n |sed ' s/[0-9]//g ' if [-N ' $n 1 "]then echo" please input a number. "Exit 1fiif [$n-lt] && [$n-ge 0]then tag=1elif [$n-ge] && [$n-lt +]then tag=2elif [$n-ge] && [$n-lt ]then Tag=3elif [$n-ge] && [$n-le]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
[[email protected] shell]# sh -x case1.sh + read -p ‘Please input a number: ‘ nPlease input a number: 101+ ‘[‘ -z 101 ‘]‘++ echo 101++ sed ‘s/[0-9]//g‘+ n1=+ ‘[‘ -n ‘‘ ‘]‘+ ‘[‘ 101 -lt 60 ‘]‘+ ‘[‘ 101 -ge 60 ‘]‘+ ‘[‘ 101 -lt 80 ‘]‘+ ‘[‘ 101 -ge 80 ‘]‘+ ‘[‘ 101 -lt 90 ‘]‘+ ‘[‘ 101 -ge 90 ‘]‘+ ‘[‘ 101 -le 100 ‘]‘+ tag=0+ case $tag in+ echo ‘The number range is 0-100.‘The number range is 0-100.
[[email protected] shell]# sh -x case1.sh + read -p ‘Please input a number: ‘ nPlease input a number: 78+ ‘[‘ -z 78 ‘]‘++ echo 78++ sed ‘s/[0-9]//g‘+ n1=+ ‘[‘ -n ‘‘ ‘]‘+ ‘[‘ 78 -lt 60 ‘]‘+ ‘[‘ 78 -ge 60 ‘]‘+ ‘[‘ 78 -lt 80 ‘]‘+ tag=2+ case $tag in+ echo okok
Shell script logic judgment, file directory attribute judgment, If,case usage