日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

shell腳本語言之if條件判斷語句實例詳解_linux shell

作者:小鵬linux ? 更新時間: 2022-06-26 編程語言

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目錄已存在,正在退出..."

2.雙分支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不存在的錯,但不影響結果。)

3.多分支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

總結

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

欄目分類
最近更新