20.5 Shell脚本中的逻辑判断

格式1:if 条件 ; then 语句; fi
格式2:if 条件; then 语句; else 语句; fi
格式3:if …; then … ;elif …; then …; else …; fi
逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then

操作过程

[root@linux-01 shell]# vim if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi

[root@linux-01 shell]# sh if1.sh 
ok

[root@linux-01 shell]# vim if2.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok else echo nook fi

[root@linux-01 shell]# sh if2.sh 
ok

[root@linux-01 shell]# vim if2-2.sh #!/bin/bash a=2 if [ $a -gt 3 ] then echo ok else echo nook fi

[root@linux-01 shell]# sh if2-2.sh 
nook

[root@linux-01 shell]# vim if3.sh #!/bin/bash a=5 if [ $a -gt 6 ] then echo ">1" elif [ $a -lt 6 ] then echo "<6 && >1" else echo nook fi

[root@linux-01 shell]# sh if3.sh 
<6 && >1

20.6 文件目录属性判断

[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行

操作过程

[root@linux-01 shell]# vim file1.sh #!/bin/bash f="/tmp/aminglinux" if [ -f $f ] then echo $f exist. else touch $f fi

[root@linux-01 shell]# sh -x file1.sh 
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ touch /tmp/aminglinux

[root@linux-01 shell]# sh -x file1.sh 
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ echo /tmp/aminglinux exist.
/tmp/aminglinux exist.

[root@linux-01 shell]# vim file2.sh #!/bin/bash f="/tmp/aminglinux" if [ -d $f ] then echo $f exist. else mkdir $f fi

[root@linux-01 shell]# sh -x file2.sh 
+ f=/tmp/aminglinux
+ '[' -d /tmp/aminglinux ']'
+ mkdir /tmp/aminglinux

[root@linux-01 shell]# sh -x file2.sh 
+ f=/tmp/aminglinux
+ '[' -d /tmp/aminglinux ']'
+ echo /tmp/aminglinux exist.
/tmp/aminglinux exist.

[root@linux-01 shell]# vim file3.sh #!/bin/bash f="/tmp/aminglinux" if [ -e $f ] then echo $f exist. else mkdir $f fi

[root@linux-01 shell]# sh -x file3.sh 
+ f=/tmp/aminglinux
+ '[' -e /tmp/aminglinux ']'
+ echo /tmp/aminglinux exist.
/tmp/aminglinux exist.

[root@linux-01 shell]# vim file4.sh #!/bin/bash f="/tmp/aminglinux" if [ -r $f ] then echo $f readable. fi

[root@linux-01 shell]# sh -x file4.sh 
+ f=/tmp/aminglinux
+ '[' -r /tmp/aminglinux ']'
+ echo /tmp/aminglinux readable.
/tmp/aminglinux readable.

[root@linux-01 shell]# vim file5.sh #!/bin/bash f="/tmp/aminglinux" if [ -w $f ] then echo $f writeable. fi

[root@linux-01 shell]# sh -x file5.sh 
+ f=/tmp/aminglinux
+ '[' -w /tmp/aminglinux ']'
+ echo /tmp/aminglinux writeable.
/tmp/aminglinux writeable.

[root@linux-01 shell]# vim file6.sh #!/bin/bash f="/tmp/aminglinux" if [ -x $f ] then echo $f exeable. fi

[root@linux-01 shell]# sh -x file6.sh  
+ f=/tmp/aminglinux
+ '[' -x /tmp/aminglinux ']'
+ echo /tmp/aminglinux exeable.
/tmp/aminglinux exeable.

20.7 if特殊用法

if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… 
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

操作过程

[root@linux-01 shell]# vim file4.sh #!/bin/bash if [ ! -f /tmp/lalala ] then echo "/tmp/lalala not exist." exit fi n='wc -l /tmp/lalala' if [ -z "$n" ] then echo error exit elif [ $n -gt 100 ] then echo aljalala fi

[root@linux-01 shell]# sh -x file4.sh 
+ '[' '!' -f /tmp/lalala ']'
+ echo '/tmp/lalala not exist.'
/tmp/lalala not exist.
+ exit



[root@linux-01 shell]# if [ -n if1.sh ]; then echo ok; fi
ok

[root@linux-01 shell]# if [ -n "$b" ]; then ehco $b; else echo "b is null"; fi
b is null


[root@linux-01 shell]# if grep -wq 'user1' /etc/passwd; then echo "user1 exist";fi
user1 exist

20.8-9 case判断

格式 case  变量名 in  value1)    command    ;; value2)    command   ;; *)  commond ;; esac

在case程序中,可以在条件中使用|,表示或的意思, 比如 2|3) command ;;

shell脚本案例

#!/bin/bash read -p "Please input a number: " n if [ -z "$n" ] then echo "Please input a number." exit 1 fi

n1=echo $n|sed 's/[0-9]//g' if [ -n "$n1" ] then echo "Please input a number." exit 1 fi

if [ $n -lt 60 ] && [ $n -ge 0 ] then tag=1 elif [ $n -ge 60 ] && [ $n -lt 80 ] then tag=2 elif [ $n -ge 80 ] && [ $n -lt 90 ] then tag=3 elif [ $n -ge 90 ] && [ $n -le 100 ] then tag=4 else tag=0 fi

case $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The number range is 0-100." ;; esac

操作过程

[root@linux-01 shell]# vim case.sh 输入上面的样本

[root@linux-01 shell]# sh case.sh 
Please input a number: 50
not ok
[root@linux-01 shell]# sh case.sh 
Please input a number: 120
The number range is 0-100.