shell if语句

流程控制:if

单分支结构

if 条件测试

then命令序列

fi

双分支结构

if 条件测试

then命令序列

else命令序列

fi

多分支结构

if 条件测试1

then命令序列

[elif 条件测试2

then 命令序列

elif 条件测试3

then 命令序列]...

else 命令序列

fi

#!/usr/bin/env bash

read -p "Please input username" user

if id $user &>/dev/null;then
    echo "user $user already exists"
else
    useradd $user
    if [ $? -eq 0 ];then
        echo "$user is created"
    fi
fi
[root@localhost ~]# cat apache.sh 
#!/usr/bin/env shell
#conn test
ping -c1 192.168.244.130 &>/dev/null
if [ $? -eq 0 ];then
    yum install httpd
    service start httpd
    chkconfig httpd on    
    curl http://127.0.0.1 &>/dev/null
    if [ $? -eq 0 ];then
        echo "Apache ok..."
fi
elif ping -c1 192.168.244.254 &>/dev/null;then
    echo "192.168.244.130 is down"
else
    echo "check ip address & gateway!"
fi
原文地址:https://www.cnblogs.com/weiwenbo/p/6709267.html