Shell中的if判断怎么用?

admin 2023年2月15日10:37:15评论20 views字数 3507阅读11分41秒阅读模式

原文:https://blog.csdn.net/weixin_46659843/article/details/124139867

1. 单分支 if 条件语句

then 后面跟符合条件之后执行的程序,可以放在 [] 之后,用; 分隔。也可以换行写入,就不需要 “;” 了。

比如:

if   [  条件判断式  ] 
    then
        程序 
fi

1.1 举例:判断目录是否存在,不存在则创建

#!/bin/bash
#date:2022-4-13
#描述:if单分支语句示例,判断目录是否存在
read -p "请输入需要判断的目录:" name
if [ ! -d $name ]
    then
     echo "$name目录不存在,正在创建..."
     mkdir -p $name
     echo "$name目录创建完成."
fi
     echo "$name目录已存在,正在退出..."

  1. 双分支 if 条件语句


if   [  条件判断式  ] 
    then
        条件成立时,执行的程序。 
    else
        条件不成立时,执行的另一个程序。 
fi

2.1 举例:监听并自动重启 apache 服务脚本

在日常工作中,服务器上的服务经常会宕机。如果我们对服务器监控不好,就会造成服务器中服务宕机了,而管理员却不 知道的情况。这是我们可以写一个脚本来监听本机的服务。如果服务停止或宕机了,可以自动重启这些服务。用 apache 举例:

首先介绍端口扫描命令,nmap 端口扫描命令,

格式:nmap -sT 域名或 IP

子选项:

-s         扫描 -T        扫描所有开启的 TCP 端口

nmap 扫描后显示的端口一定是存活的。

脚本要使用 nmap 命令,首先用 yum -y install nmap 安装。

apache 服务也是 yum 安装。

[root@xiaopeng ~]# cat autostart.sh 
#!/bin/bash 
port=$(nmap -sT 192.168.22.222 | grep tcp | grep http | awk '{print $2}'
if [ "$port" == "open" ] 
    then 
        echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log 
    else 
        /etc/rc.d/init.d/httpd start &> /dev/nullecho "$(date) restart httpd!!" >> /tmp/autostart-err.log 
fi

(首先用 nmap 命令查看是否开启 apache 并赋值给 port。

然后进行条件判断。如果服务开启,输出当前时间 + httpd is ok 到 / tmp/autostart-acc.log。

如果变量 port 的值不是 open,那么执行 else 下操作。首先启动 apache 服务,将启动后 信息输出至位桶,然后在 / tmp/autostart-err.log 中记录。在本次脚本中 nmap 命令使用的是

IP 查找端口,但并未指 DNS,所以会报 DNS 不存在的错,但不影响结果。)

  1. 多分支 if 条件语句


if   [  条件判断式1  ] 
    then
        当条件判断式1成立时,执行程序1。 
elif  [  条件判断式2  ] 
    then
        当条件判断式2成立时,执行程序2。 
        ......(可加入更多条件) 
    else
        当所有条件不成立时,最后执行此程序。 
fi

3.1 举例:判断用户输入的是文件还是目录

#!/bin/bash
#date:2022-4-13
#描述:判断文件类型
read -p "请输入一个文件:" file
if [ -z $file ]
    then
     echo "错误!输入的文件为空."
elif [ ! -e $file ]
    then
     echo "错误!输入的文件不存在."
elif [ -f $file ]
    then
     echo "$file是一个普通文件"
elif [ -d $file ]
    then
     echo "$file是一个目录"
else
     echo "$file是其他类型文件"
fi

4.case 条件语句

多分支 case 条件语句

case  $变量名  in 
    “值1”) 
        如果$变量等于值1,则执行程序1 
    ;; 
    “值2”) 
        如果$变量等于值2,则执行程序2 
    ;; 
        ....省略... 
    *) 
        如果$变量的值不是以上值,则执行此程序 
    ;; 
esac

4.1 举例:创建启动脚本,让 service 命令管理 apache

[root@xiaopeng htdocs]# vim /etc/init.d/apached
#!/bin/bash
# chkconfig: 2345 64 36         
# description: A very fast and reliable SQL database engine
httpd=/usr/local/apache2/bin/apachectl
case $1 in
start)
        $httpd start
        ;;
stop)
        $httpd stop
        ;;
restart)
        $0 stop                 
        sleep 0.05              
        $0 start
        ;;
configtest)                     
$httpd  -t
;;
*)
        echo "usage:$0 start|stop|restart|configtest."
        ;;
esac

4.2 举例:创建启动脚本,让 service 命令管理 nginx

[root@xiaopeng conf]# vim  /etc/init.d/nginx
#!/bin/bash
#Author:liu
#chkconfig: 2345 99 33
#description: nginx server control tools
 
ngxc="/usr/local/nginx/sbin/nginx"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
case "$1" in
    start)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                $ngxc
                $ngxc_fpm
                echo "nginx service start success!"
        else
                $ngxc -t
        fi
        ;;
    stop)
        $ngxc  -s  stop
        killall  php-fpm
        echo "nginx service stop success!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
               $ngxc  -s  reload
                pkill  -HUP  php-fpm
                echo "reload nginx config success!"
        else
                $ngxc -t
        fi
        ;;
    *)
        echo "please input stop|start|restart|reload."
        exit 1
esac


注:如有侵权请联系删除

Shell中的if判断怎么用?

   学习更多技术,关注我:   

觉得文章不错给点个‘再看’吧

原文始发于微信公众号(编码安全研究):Shell中的if判断怎么用?

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年2月15日10:37:15
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Shell中的if判断怎么用?https://cn-sec.com/archives/1283091.html

发表评论

匿名网友 填写信息