一、shell編程之if語句
引言
Linux在shell編程中,通常都是以自上而下運行,但是為了提高其代碼嚴謹性,我們即引入了多條件 控制語句例如:if、for、while、case等語句,有時候針對條件我們還會結合正則表達式去運用。將這些流程控制語句進行熟練掌握,必然對shell腳本編寫有很大的幫助
if表達式結構(三種)
結構形式 | 語法 |
單表達式 | if 表達式;then ? ? ? ? 語句1 fi |
雙表達式 | if 表達式;then ? ? ? ? 語句1 else: ? ? ? ? 語句2 fi |
多表達式 | if 表達式;then? ? ? ? ? 語句1 elif 表達式 ? ? ? ? 語句2 else ? ? ? ? 語句3 fi |
二、示例分析
大小比較分析單分支、對分支語法
### if單語句#####引用單個數字比較
#!/bin/bash if (( 5 > 2 ));then echo "hello"###引用變量比較#!/bin/bash
$NUM1="$1"
$NUM2="$2"if (( $NUM1 > $NUMM2 ));then echo "=================="echo "$NUM1 greter than $NUM2"
fi## if多條件和雙條件判斷###引用變量比較#!/bin/bash
NUM1=$1
NUM2=$2if (( $NUM1 > $NUM2 ));then echo "=================="echo "$NUM1 greter than $NUM2"
else if [ $NUM1 == $NUM2 ];thenecho "$NUM1 is equal $NUM2"exitfiecho "$NUM1 less than $NUM2"
fi
除了上述表達式可以用((表達式))之外,還可以使用test命令來表示表達式,除次之外還能判斷文件是否存在(-f 參數? ?-d參數),我們通常會將test? =? [? ] 表示為等效的,【 】中不支持數學運算(>、<、=),但是可以用(gt、lt、eq)
上面的$?表示若執行結果為真那么返回結果為0,但如果非真,即結果不為0
我這里使用了test 的另一種等效寫法做了修改(【? ?】)
#!/bin/bash
NUM1=$1
NUM2=$2if [ $NUM1 -gt $NUM2 ];then echo "=================="echo "$NUM1 greter than $NUM2"
else if [ $NUM1 -eq $NUM2 ];thenecho "$NUM1 is equal $NUM2"exitfiecho "$NUM1 less than $NUM2"
fi
雖然一個[ express] 和[ [ express ]?],中的表達式都能用-eq、-gt、lt,但是在邏輯關系表示時的情況確截然不同
[ [ express ] ]中只能填寫&&(邏輯與) 和 ||(邏輯或)
[ express] 只能填寫-a(邏輯與),-o(邏輯或)
創建不存在的目錄
#!/bin/bash
if [ ! -d /tmp/hello ];thenmkdir -p /tmp/helloecho "ok!!!!"
fi
if 多個條件測試判斷成績
#!/bin/bash
NUM=$1
if [ $NUM -ge 100 ];thenecho "vary good."elseif [ $NUM -ge 85 ];thenecho "good."elseif [ $NUM -ge 60 ];thenecho "pass."elseecho "no pass."fifi
fiNUM=$1
if [ $NUM -ge 100 ];thenecho "vary good."
elif [ $NUM -ge 85 ];thenecho "good."
elif [ $NUM -ge 60 ];thenecho "pass."
elseecho "no pass."
Nginx之v3腳本優化引用(位置參數)
嚴格規范參數表達
vim可視化塊快捷使用技巧
(ctrl +v --------- 上下鍵選擇----------大寫I----------------修改----------- 2 Esc)
## 對help參數和位置參數的限制if [ $1 == "help" -o $1 == "-h"];then echo -e "\033[32m-----------------\033[0m"echo -e "\033[32mUsage:{/bin/sh $0 1.24.0|1.26.1|help}\033[0m"exit
fi## 避免重復性安裝軟件包
CHECK_NUM=$(rpm -qa | grep -wE "gcc|pcre-devel|zlib-devel|wget"|wc -l)
if [ $CHECK_NUM -lt 4 ];thenyum -y install wget tar make gccyum -y install pcre-devel zlib-devel openssl-devel
fi## 避免重復性編譯,對于文件不存在編譯安裝if [ ! -f $NGX_DIR/sbin/nginx ];then
CHECK_NUM=$(rpm -qa | grep -wE "gcc|pcre-devel|zlib-devel|wget"|wc -l)
if [ $CHECK_NUM -lt 4 ];thenyum -y install wget tar make gccyum -y install pcre-devel zlib-devel openssl-devel
fi## 若軟件已部署,提示已經部署了(若存在軟件文件夾)
else echo -e "\033--軟件已經部署完成!!!!!無需重復部署,請輸入是否重裝?yes/no--\033[0m"ls -ld $NGX_DIRll $NGX_DIRread INPUT if [ $INPUT == "yes" -o $INPUT == "Y" ];then重裝邏輯## 由于防火墻報錯問題,需要先關閉nginx,然后對防火墻對外端口進行判斷CHECK_FIRE=$(firewalld-cmd --list-all | grep -w "80" | wc -l)if [ $CHECK_FIRE -ne 1 ];thenfirewalld-cmd -add-port=80/tcp --permanentsystemctl reload firewalld.service
fi
如果想要對預編譯、編譯結果進行隱藏,可以使用黑洞文件 >> /dev/null 2>&1(這里2表示標準輸出。1表示錯誤輸出)
三、條件判斷符號的使用
單引號和雙引號本質區別在于內置變量引用必須使用雙引號,單引號默認是字符串格式
$( command )? ? 和`command`執行腳本命令結果是一樣的
Nginx:v4版本更新之虛擬主機(多網站)的添加
虛擬主機完成構建
echo -e "\033[32m----正在配置虛擬主機--$NGX_VHOST-----\033[0m"
## 切換到Nginx目錄
cd $NGX_DIR/conf## 備份Nginx配置文件
\cp nginx.conf nginx.conf.bak
echo "
worker_processes 1;
events {worker_connections 1024;
}
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;include domains/*;
}
">nginx.conf## 創建虛擬主機配置文件目錄mkdir -p domains/## cd切換到domains目錄
cd domains/echo "
server{listen 80;server_name v1.nginx;location / {root /data/webapp/v1.nginx;index index.html index.htm index.php;}
}">v1.nginx.conf## 創建虛擬主機發布目錄,寫入測試頁面######## 多虛擬主機創建 添加位置參數$1($NGX_VHOST)
mkdir -p /data/webapp/v1.nginx
echo "v1.nginx test pages." > /data/webapp/v1.nginx/index.html## 判斷是否重啟
$NGX_DIR/sbin/nginx -t > /dev/null 2>&1if [ $? -eq 0 ];then
## 重啟nginx服務$NGX_DIR/sbin/nginx -s reload
fi
cat $NGX_VHOST
echo -e "\033[32m------------------\033[0m"
echo -e "\033[32m----nginx虛擬主機--$NGX_VHOST-----添加成功\033[0m"
添加成功效果如下:
添加nginx_host虛擬主機腳本位置優化之后腳本
#!/bin/bash# ***************************************************************************
# *
# * @file:auto_install_nginx_v1.sh
# * @author:www.hxd666.cn
# * @date:2025-07-22 09:47
# * @version 1.0
# * @description: Shell script
# * @Copyright (c) all right reserved
#*
#**************************************************************************/ NGX_VER="$1"
NGX_URL="http://nginx.org/download"
NGX_SOFT="nginx-${NGX_VER}.tar.gz"
NGX_DIR="/usr/local/nginx"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"
NGX_SRC=$(echo $NGX_SOFT | sed 's/.tar.gz//g')
NGX_VHOST=$1
## 創建虛擬主機echo -e "033[32m---------正在配置虛擬主機------033[32m"cd $NGX_DIR/conf\cp nginx.conf nginx.conf.bak## 創建nginx配置文件
echo "
worker_processes 1;
events {worker_connections 1024;
}
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;include domains/*;
}" > nginx.conf## 創建 虛擬主機配置文件
mkdir -p domains/cd domainsecho "server{listen 80;server_name $NGX_VHOST;location / {root /data/webapp/$NGX_VHOST;index index.html index.htm index.php;}
}
" > $NGX_VHOST.conf## 創建虛擬主機目錄mkdir -p /data/webapp/$NGX_VHOST
echo "$NGX_VHOST test pages." > /data/webapp/"$NGX_VHOST"/index.html## 判斷是否重啟if [ $? -eq 0 ];then $NGX_DIR/sbin/nginx -s reload
ficat $NGX_VHOST.confecho "033[32m----------nginx 虛擬主機--$NGX_VHOST-----添加成功!!!!"