if;脚本中退出语句:exit 数字,用$?查时为exit设置的数字,此数字为程序执行完后的返回数据,可以通过此方法自动设定

if [ 条件 ];then

  代码

fi

if [ 条件 ]

  then

    代码

fi

[root@localhost ~]# df
文件系统          1K-块    已用     可用 已用% 挂载点
/dev/sda5      16558080 1443696 15114384    9% /
devtmpfs         490168       0   490168    0% /dev
tmpfs            499968       0   499968    0% /dev/shm
tmpfs            499968    6872   493096    2% /run
tmpfs            499968       0   499968    0% /sys/fs/cgroup
/dev/sdb1        999320    2564   927944    1% /disk1
/dev/sdb5       1998672    6144  1871288    1% /disk5
/dev/sda2       2086912   33160  2053752    2% /home
/dev/sda1        201380  116576    84804   58% /boot
tmpfs             99996       0    99996    0% /run/user/0
[root@localhost ~]# df|grep sda5
/dev/sda5      16558080 1443696 15114384    9% /
[root@localhost ~]# df|grep sda5|awk '{print $5}'
9%
[root@localhost ~]# df|grep sda5|awk '{print $5}'|cut -d % -f1
9
[root@localhost ~]# ls
shFiles  testfile  testfileb
[root@localhost ~]# vim myShell.sh  注释:以下为shell脚本代码
#!/bin/bash

#统计根分区使用率

rate=$(df|grep /dev/sda5|awk '{print $5}'|cut -d % -f1)

if [ $rate -ge 80 ];then  注释:[ $rate -ge 80 ]可写成test $rate -ge 80
        echo "/dev/sda5 is full!"
fi

 if [ 条件 ]

  then

    执行语句

  else

    执行语句

fi

 

if [ 条件 ]

  then

    执行语句

elif [ 条件 ]

  then

    执行语句

...

elif  [ 条件 ]

  then

    执行语句

  else

    执行语句

fi

posted @ 2017-08-15 20:45  xiongjiawei  阅读(490)  评论(0编辑  收藏  举报