一、shell脚本中的逻辑判断

1、判断语句if

a)不带else

格式:

if 判断语句;then 
   command
fi

实例:

[root@zlinux-01 shell]# vim if01.sh          //判断数值大小第一种方法用[],注意前后空格

#! /bin/bash
a=5
if [ $a -gt 3 ];then
    echo "这个数字大于3"
fi
#-gt:大于,-lt:小于,-ge:大于或等于,-le:小于或等于,-eq:等于,-ne:不等于

[root@zlinux-01 shell]# sh if01.sh 
这个数字大于3
[root@zlinux-01 shell]# vim if02.sh                //判断数值大小的第二种方式:(())

#! /bin/bash
a=5
if ((a>3));then
   echo "这个数字大于3"
fi
#((a>3)),双括号是shell中的特有格式,只用一个括号或者不用都会报错

[root@zlinux-01 shell]# sh if02.sh 
这个数字大于3

b)带有else

格式:

if 判断语句;then 
   command
else
   command
fi

实例:

[root@zlinux-01 shell]# vim ifelse.sh 

#! /bin/bash
read -p "请输入分数:" a
if [ $a -lt 60 ];then
   echo "你没有通过测试!"
else
   echo "恭喜你,顺利通过测试!"
fi
#read命令用于和用户交互,它把用户输入的字符串作为变量值 

[root@zlinux-01 shell]# sh ifelse.sh 
请输入分数:60
恭喜你,顺利通过测试!
[root@zlinux-01 shell]# sh ifelse.sh 
请输入分数:59
你没有通过测试!

c)带有elif

格式:

if 判断语句1;then
   command
elif 判断语句2;then
   command
else
   command
fi

实例:

[root@zlinux-01 shell]# vim elif.sh 

#! /bin/bash
read -p "请输入分数:" a
if (($a<60));then
   echo "未通过测试。"
elif ((a>=60))&&(($a<85));then
   echo "通过测试,成绩良好。"
else
   echo "通过测试,成绩优秀!"
fi
# &&表示并且,当然也可以用||表示或者

[root@zlinux-01 shell]# sh elif.sh 
请输入分数:60
通过测试,成绩良好。
[root@zlinux-01 shell]# sh elif.sh 
请输入分数:34
未通过测试。
[root@zlinux-01 shell]# sh elif.sh 
请输入分数:99
通过测试,成绩优秀!

2、和文档相关的判断

shell中if嗨经常用于判断文档属性。常用选项有:

-e:判断文件或目录是否存在;
-d:判断是不是目录以及是否存在;
-f:判断是否是文件以及是否存在;
-r:判断是否有读权限;
-w;判断是否有写权限;
-x:判断是否有执行权限。

格式:

if [ -e filename ];then
   command
fi

实例:

[root@zlinux-01 shell]# if [ -d /home/ ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -f /home/ ]; then echo ok; fi                //home是目录不是文件,所以没有输出ok
[root@zlinux-01 shell]# touch /root/test.txt
[root@zlinux-01 shell]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -x /root/test.txt ]; then echo ok; fi
[root@zlinux-01 shell]# if [ -e /root/test1.txt ]; then echo ok; fi
[root@zlinux-01 shell]# if [ -d /root/test.txt ]; then echo ok; fi

3、case逻辑判断

格式:

case 变量 in
value1)
              command
                            ;;
value2)
              command
                            ;;
value3)
              command
                            ;;
*)
              command
                            ;;
esac

不限制value的个数,*表示其他值
实例:

[root@zlinux-01 shell]# vim case.sh

#! /bin/bash
read -p "请输入一个数字:" n
a=$[$n%2]
case $a in
  1)
      echo "这数字是奇数"
      ;;
  0)
      echo "这数字是偶数"
      ;;
  *)
      echo "这个不是数字"
      ;;
esac

[root@zlinux-01 shell]# sh case.sh
请输入一个数字:3
这数字是奇数
[root@zlinux-01 shell]# sh case.sh
请输入一个数字:2
这数字是偶数

4、练习(输入考试成绩,判断得分等级)

[root@zlinux-01 shell]# vim test01.sh

#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
#“exit 1”表示执行该部分命令后的返回值
#即,命令执行完后使用echo $?的值
fi

n1=`echo $n|sed 's/[0-9]//g'`
#判断用户输入的字符是否为纯数字
#如果是数字,则将其替换为空,赋值给$n1
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
#判断$n1不为空时(即$n不是纯数字)再次提示用户输入数字并退出
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
#tag的作用是为判断条件设定标签,方便后面引用
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@zlinux-01 shell]# sh test01.sh 
Please input a number: aa
Please input a number.
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 78
ok
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 90
oook
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 11
not ok