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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章