shell脚本中的逻辑判断

不带else

[root@aminglinux-128 ~]# vim if1.sh
[root@aminglinux-128 ~]# sh if1.sh
Please input your score: 90
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if1.sh
[root@aminglinux-128 ~]# sh if1.sh
Please input your score: 33
You didn't pass the exam.

上例中编辑的脚本

#! /bin/bash
read -p "Please input your score: "33
if ((a<60)); then
    echo "You didn't pass the exam."
fi
#! /bin/bash
read -p "Please input your score: "90
if ((a<60)); then
    echo "You didn't pass the exam."
fi

带有else

#! /bin/bash
read -p "Please input your score: "80
if ((a<60)); then
    echo "You didn't pass the exam."
else
    echo "Good! You passed the exam."
fi
#! /bin/bash
read -p "Please input your score: "25
if ((a<60)); then
    echo "You didn't pass the exam."
else
    echo "Good! You passed the exam."
fi

脚本执行结果

[root@aminglinux-128 ~]# sh if2.sh
Please input your score: 25
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if2.sh
[root@aminglinux-128 ~]# sh if2.sh
Please input your score: 80

带有elif

vim 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)); then
    echo "Good! You pass the exam."
else
    echo "Very good! Your score is very high!"
fi

脚本输出结果

[root@aminglinux-128 ~]# vim if3.sh
[root@aminglinux-128 ~]# sh if3.sh
please input your score: 90
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if3.sh
[root@aminglinux-128 ~]# sh if3.sh
please input your score: 60
You didn't pass the exam.

和文档相关的判断

shell脚本中的if用于判断文档的属性,比如判断是普通文件还是目录,判断文件是否有读、写、执行权限。

-e:判断文件或目录是否存在

-d:判断是不是目录以及是否存在

-f:判断是不是普通文件以及是否存在

-r:判断是否有读权限

-w:判断是否有写权限。

-x:判断是否可执行。

使用if判断时的具体格式:

if [ -e filename ] ; then

command

fi

示例 

[root@aminglinux-128 ~]# if [ -d /home/ ];then echo ok; fi
ok
[root@aminglinux-128 ~]# if [ -f /home/ ];then echo ok; fi

因为/home/是目录而非文件,所以并不会显示ok。

[root@aminglinux-128 ~]# if [ -f /home/ ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -f /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -r /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -w /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -x /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -e /root/text.txt ];then echo ok; fi

case逻辑判断

格式为

case   变量  in

value1)

                 command

                 ;;

value2)

                 command

                 ;;

value3)

                 command

                 ;;

*)                   

                 command

                 ;;

esac

上面的结构中,不限制value的个数,*代表其他值。下面写一个判断输入数值是奇数还是偶数的脚本。

vim case.sh
#! /bin/bash
read -p "Input anumber: " n
a=$[$n%2]
case $a in
1)
          echo "The number is odd."
          ;;
0)
          echo "The number is even."
          ;;
*)
          echo "It's not a number!"
          ;;
esac

脚本中$a的值为1或0,执行结果如下:

[root@aminglinux-128 ~]# sh case.sh
Input anumber: 100
The number is even.
[root@aminglinux-128 ~]# sh case.sh
Input anumber: 101
The number is odd.

case脚本常用于编写系统服务的启动脚本。