shell编程-双分支if语句-定时任务-多分支

[root@localhost shelltest]# ./ifdir.sh 
Please input your direct:  /home
you are right!

#!/bin/bash

read -p "Please input your direct:  " -t 10 d
if [ -d "$d" ]
        then
        echo "you are right!"
        else
        echo 
        "It is not a direct"
fi

在这里插入图片描述

  • 什么是输出重定向:https://blog.csdn.net/liucy007/article/details/90207830
  • grep 取反命令 grep -v
#!/bin/bash

apa_file=`ps aux | grep tomcat | grep -v grep`

if [ -n "$apa_file" ]
        then
          echo "$(date) apache is ok!" >> /opt/log/right_log/apache.log
        else
          echo "$(date) apache is error" >> /opt/log/error_log/apache.log
fi
ngi_file=`ps aux | grep nginx | grep -v grep`
if [ -n "$ngi_file" ]
        then
          echo "$(date) nginx is ok" >> /opt/log/right_log/nginxtest.log
        else
          systemctl start nginx &> /dev/null
          echo "$(date) start nginx !!" >> /opt/log/error_log/nginxtest.log
fi

[root@localhost log]# cd error_log/
[root@localhost error_log]# ls
apache.log  nginxtest.log
[root@localhost error_log]# vim apache.log 
[root@localhost error_log]# vim nginxtest.log 
[root@localhost error_log]# ps aux | grep nginx
root       7469  0.0  0.1  46344  1012 ?        Ss   16:17   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      7470  0.0  0.1  46788  1972 ?        S    16:17   0:00 nginx: worker process
root       7480  0.0  0.0 112708   972 pts/0    R+   16:19   0:00 grep --color=auto nginx
[root@localhost error_log]# 

  • 停掉nginx服务再执行一遍脚本,查看变化
[root@localhost error_log]# ps aux | grep nginx
root       7510  0.0  0.0 112708   976 pts/0    R+   16:20   0:00 grep --color=auto nginx
[root@localhost error_log]# /bin/bas
base64      basename    bash        bashbug     bashbug-64  
[root@localhost error_log]# /bin/bas
base64      basename    bash        bashbug     bashbug-64  
[root@localhost error_log]# /bin/bash /opt/shelltest/ifapache.sh 
[root@localhost error_log]# ps aux | grep nginx
root       7528  0.0  0.1  46344  1008 ?        Ss   16:21   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      7529  0.0  0.1  46788  1968 ?        S    16:21   0:00 nginx: worker process
root       7532  0.0  0.0 112708   972 pts/0    R+   16:21   0:00 grep --color=auto nginx
[root@localhost error_log]#
  • 上面这个脚本有一个漏洞

定时执行脚本

  • https://blog.csdn.net/ycf921244819/article/details/80520217

多分支

在这里插入图片描述

[root@localhost shelltest]# vim iffile.sh
[root@localhost shelltest]# ./iffile.sh 
please input a file nameiffull.sh
your are right!!!
[root@localhost shelltest]# 

  • echo $?查看上一条命令是否正确执行
[root@localhost shelltest]# ./iffile.sh 
please input a file nameyyyy
your input is a anther file
[root@localhost shelltest]# ./iffile.sh 
please input a file name/home
your input is a direct!!!
[root@localhost shelltest]# 

特别提醒:

  • 在脚本里面read读取数据的时候,我们没法直接用删除键(Backpace)删除,这时候按ctrl+删除键就可以删除。
  • 第二,写else语句的时候,没有then
[root@localhost shelltest]# ./iffile.sh 
please input a file namesfdsafd^H^H^H^Hplease input content!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值