菜比之路

走菜比的路,让大牛说去吧
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

【shell】if语句

Posted on 2016-06-20 15:19  毕加索的ma  阅读(204)  评论(0编辑  收藏  举报

单分支:

 

#!/bin/bash

rate=$(df -h|grep vg_andon-lv_root |awk '{print $5}'| cut -d "%" -f1)
#echo $rate
if [ $rate -ge 8  ]
        then
                echo "root partition is full"
fi

  

 

双分支:

 

#!/bin/bash
status=$(nmap -sT 192.168.69.20 |grep tcp | grep '\<http\>' |awk '{print $2 }') ##扫描远程目标机器开启的服务  若用ps 或者netstat不准确,会出现进行还在但是D住的状态
echo $status
if [ $status == open ]
        then
                echo "apache is running at $(date)" >> /root/apache-acc.log
        else
                service httpd start & > /dev/null
                echo "apache is restarting at $(date)" >> /root/apache-err.log
fi

 

多分支:

 

read -p  "please input filename:" file
if [ -z $file ]
        then
                echo "your input is null"
                exit 1
elif [ ! -e $file ]
        then
                echo "file  not exist"
                exit 2
elif [ -f $file ]
        then
                echo 'file is file'
elif [  -d $file ]
        then
                echo 'file is directory'
else
        echo "file is other "
fi