Shell编程(二)-if判断及特殊用法,文件目录属性判断,case判断

[toc]linux

Shell编程(二)

1、shell脚本中的逻辑判断

1.1 判断语句if

1.1.1 格式1:

if 判断语句;then 
   command
fi

示例1shell

# vim if01.sh          //判断数值大小第一种方法用[],注意先后空格
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
   echo ok
fi
[root@xavilinux ~]# sh if01.sh
ok
  • [ ] -gt:大于,
  • [ ] -lt:小于,
  • [ ] -ge:大于或等于,
  • [ ] -le:小于或等于,
  • [ ] -eq:等于,
  • [ ] -ne:不等于

1.1.2 格式2:

格式:

if 判断语句;then 
   command
else
   command
fi
[root@xavilinux ~]# vim if02.sh
#!/bin/bash
a=2
if [ $a -gt 3 ]
then
   echo ok
else
   echo nook
fi

查看执行过程编程

[root@xavilinux ~]# sh -x if02.sh
+ a=2
+ '[' 2 -gt 3 ']'
+ echo nook
nook

1.1.3 格式3:

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

示例:vim

#!/bin/bash
read -p "请输入考试分数:" a
if [ $a -lt 60 ]
then
   echo "重考,未经过考试"
elif [ $a -gt 60 ] && [ $a -lt 85 ]
then
   echo "经过考试,成绩良好"
else
   echo "经过考试,成绩优秀! "
fi
[root@xavilinux ~]# sh if03.sh
请输入考试分数:80
经过考试,成绩良好

[root@xavilinux ~]# sh if03.sh
请输入考试分数:97
经过考试,成绩优秀! 
[root@xavilinux ~]# sh if03.sh
请输入考试分数:54
重考,未经过考试

2、 文件目录属性判断

  • [ ] [ -f file ]判断是不是普通文件,且存在
  • [ ] [ -d file ] 判断是不是目录,且存在
  • [ ] [ -e file ] 判断文件或目录是否存在
  • [ ] [ -r file ] 判断文件是否可读
  • [ ] [ -w file ] 判断文件是否可写
  • [ ] [ -x file ] 判断文件是否可执行
-e filename 若是 filename存在,则为真 [ -e /var/log/syslog ]

-d filename 若是 filename为目录,则为真 [ -d /tmp/mydir ]

-f filename 若是 filename为常规文件,则为真 [ -f /usr/bin/grep ]

-L filename 若是 filename为符号连接,则为真 [ -L /usr/bin/grep ]

-r filename 若是 filename可读,则为真 [ -r /var/log/syslog ]

-w filename 若是 filename可写,则为真 [ -w /var/mytmp.txt ]

-x filename 若是 filename可执行,则为真 [ -L /usr/bin/grep ]
  • 示例1
    #!/bin/bash
    f="/tmp/xavilinux"
    if [ -f $f ]
    then
    echo $f exist
    else
    touch $f
    fi

    执行过程bash

[root@xavilinux ~]# sh -x 1.sh
+ f=/tmp/xavilinux
+ '[' -f /tmp/xavilinux ']'
+ touch /tmp/xavilinux
[root@xavilinux ~]# sh -x 1.sh
+ f=/tmp/xavilinux
+ '[' -f /tmp/xavilinux ']'
+ echo /tmp/xavilinux exist
/tmp/xavilinux exist
  • 示例2
#!/bin/bash
f="/tmp/xavilinux"
if [ -r $f ]
then
   echo $f readable

fi
[root@xavi shell]# sh file02.sh
/tmp/xavilinux readable

* 经常使用语法ide

[root@xavi shell]# vim file.sh

#!/bin/bash
f="/tmp/xavilinux"
 [ -f $f ] && rm -f $f
等同于如下:

if [ -f $f ]
then
   rm -f $f
fi

3、if特殊用法

if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样code

养成好习惯,必定要对判断的值添加 “双引号”;若是是"文件"能够省略

if [ -n "$a" ] 表示当变量a的值不为空input

[root@xavi ~]# if [ -n iftest1.sh ]; then echo ok; fi
ok
[root@xavi ~]# if [ -n "$b" ]; then echo $b; else echo
[root@xavi ~]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null

if grep -q '123' 1.txt; then  表示若是1.txt中含有'123'的行时会怎么样it

grep -w w表示word,字
grep -wq 加上q不用过滤用户,直接显示结果class

[root@xavi ~]# grep -w 'xavi' /etc/passwd
xavi:x:1000:1000:xavi,xavi's office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
[root@xavi ~]# if grep -w 'xavi' /etc/passwd; then echo "xavi exist"; fi
xavi:x:1000:1000:xavi,xavi's office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
xavi exist
[root@xavi ~]# if grep -wq 'xavi' /etc/passwd; then echo "xavi exist"; fi
xavi exist

if [ ! -e file ]; then 表示文件不存在时会怎么样

if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

4、case 判断

4.1 格式

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

若是case中的某个value是同样的,咱们能够这样写:

在case程序中,能够在条件中使用 |,表示或的意思,  
好比

2|3)     
    command  
    ;;

脚本案例

#!/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 just input a number, without any other words."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]        //通过如上的筛选,咱们来判断输入数字属于哪一个范围,而且把值交给tag
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  //大于100的状况
fi
case $tag in        //根据如上获得的值,进行判断
    1)
    echo "you didn't pass the exam!"
        ;;
    2)
        echo "good!"
        ;;
    3)
        echo "very good!"
        ;;
    4)
        echo "perfect!!!"
        ;;
    *)
        echo "Pls input a number range 0-100."
        ;; 
esac
相关文章
相关标签/搜索