【shell】if语句

单分支:

#!/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

  

原文地址:https://www.cnblogs.com/paulwinflo/p/5600614.html