bash shell中的if-then語句
shell中的流控制if語句
簡單的腳本可以只包含順序執(zhí)行的命令,但結(jié)構(gòu)化命令允許根據(jù)條件改變程序執(zhí)行的順序。
if語句
if-then語句
if-then
語句格式如下:
if command then commands fi
在其他編程語言中, if
語句之后的對象是一個等式,這個等式的求值結(jié)果為 TRUE
或 FALSE
。bash shell
的 if
語句會運行 if
后面的那個命令。如果該命令的退出狀態(tài)碼是0
,位于 then
部分的命令就會被執(zhí)行。
#!/bin/bash if pwd then echo "pwd worked" fi
輸出:
# rob@xx-rob:~$ ./test1
/home/rob
pwd worked
if-then-else 語句
格式:
if command then commands else commands fi
示例:
v=bin if grep $v pwd then echo "pwd worked" else echo "cannot find $v" fi
結(jié)果:
rob@xx-rob:~$ ./test1
# grep: pwd: 沒有那個文件或目錄
# cannot find bin
if
還可以嵌套多層:
if command1 then command set 1 elif command2 then command set 2 elif command3 then command set 3 elif command4 then command set 4 fi
test命令
bash shell if
語句的條件是command
,如果要使用常規(guī)的數(shù)值/字符串比較條件,需要使用test
命令。
使用test
命令的if-then-fi
語句:
if test condition then commands fi
如果不寫 test
命令的condition
部分,它會以非零的退出狀態(tài)碼退出,并執(zhí)行 else
語句塊。
加入條件時,test
命令會測試該條件。
bash shell
中 test
命令的另外一種寫法是使用[ condition ]
中括號,第一個方括號之后和第二個方括號之前必須加上一個空格,
否則就會報錯。
if
中條件判斷的幾個條件:
- 判斷變量是否有值
if test ${variable}
- 數(shù)值比較
- 字符串比較
- 文件比較
數(shù)值比較
test
命令的數(shù)值比較功能:
比較 | 描述 |
---|---|
n1 -eq n2 | 檢查 n1 是否與 n2 相等 |
n1 -ge n2 | 檢查 n1 是否大于或等于 n2 |
n1 -gt n2 | 檢查 n1 是否大于 n2 |
n1 -le n2 | 檢查 n1 是否小于或等于 n2 |
n1 -lt n2 | 檢查 n1 是否小于 n2 |
n1 -ne n2 | 檢查 n1 是否不等于 n2 |
#!/bin/bash if test 100 -le 145; then echo "100 is smaller than 145" fi v=12 if [ $v -eq 12 ];then echo "value is 12" fi
bash shell
只能處理整數(shù),不能使用浮點數(shù)作為判斷條件。
字符串比較
bash shell
條件測試還允許比較字符串值,比較字符串比較煩瑣。
比較 | 描述 |
---|---|
str1 = str2 | 檢查 str1 是否和 str2 相同 |
str1 != str2 | 檢查 str1 是否和 str2 不同 |
str1 < str2 | 檢查 str1 是否比 str2 小 |
str1 > str2 | 檢查 str1 是否比 str2 大 |
-n str1 | 檢查 str1 的長度是否非0 |
-z str1 | 檢查 str1 的長度是否為0 |
在bash sehll
中比較運算符需要使用轉(zhuǎn)義,否則會被當(dāng)成重定向運算符。
s1="val" s2="thi" # 升成`thi`的文件 if [ $s1 > $s2 ]; then echo "new file $v2 has been created." fi if [ $s1 \> $s2 ]; then echo "$s1 is greater than $s2." fi
比較測試中使用的是標(biāo)準(zhǔn)的ASCII
順序,根據(jù)每個字符的ASCII
數(shù)值來決定排序結(jié)果。在比較測試中,大寫字母被認(rèn)為是小于小寫字母的。
文件比較
測試Linux
文件系統(tǒng)上文件和目錄的狀態(tài)。
命令 | 描述 |
---|---|
-d file | 檢查 file 是否存在并是一個目錄 |
-e file | 檢查 file 是否存在 |
-f file | 檢查 file 是否存在并是一個文件 |
-r file | 檢查 file 是否存在并可讀 |
-s file | 檢查 file 是否存在并非空 |
-w file | 檢查 file 是否存在并可寫 |
-x file | 檢查 file 是否存在并可執(zhí)行 |
-O file | 檢查 file 是否存在并屬當(dāng)前用戶所有 |
-G file | 檢查 file 是否存在并且默認(rèn)組與當(dāng)前用戶相同 |
file1 -nt file2 | 檢查 file1 是否比 file2 新 |
file1 -ot file2 | 檢查 file1 是否比 file2 舊 |
if-then
語句允許你使用布爾邏輯來組合測試,有兩種布爾運算符可用:
[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
case
語句
在嘗試計算一個變量的值,在一組可能的值中尋找特定值,可能不得不寫出很長的 if-then-else
語句。case
命
令會采用列表格式來檢查單個變量的多個值。
case variable in pattern1 | pattern2) commands1;; pattern3) commands2;; *) default commands;; esac
一個例子:
c=1 case $c in 1 | 2) echo "1";; 3) echo "23";; esac
到此這篇關(guān)于bash shell中的if-then語句的文章就介紹到這了,更多相關(guān)shell if-then語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
獲取站點的各類響應(yīng)時間(dns解析時間,響應(yīng)時間,傳輸時間)
有時候為了測試網(wǎng)絡(luò)情況,需要返回每個階段的耗時時間,比如DNS解析耗時,建立連接所消耗的時間,從建立連接到準(zhǔn)備傳輸所使用的時間,從建立連接到傳輸開始所使用的時間,整個過程耗時,下載的數(shù)據(jù)量,下載速度,上傳數(shù)據(jù)量,上傳速度等等2014-03-03Linux?ps命令詳解及Linux查看進(jìn)程的操作方法
這篇文章主要介紹了Linux?ps命令詳解,Linux查看進(jìn)程的操作方法,ps命令常用的方式有三種,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12Linux下使用ntpdate進(jìn)行時間同步的方法實現(xiàn)
ntpdate是Linux下用于從NTP服務(wù)器同步時間的命令行工具,本文將給大家介紹Linux下使用ntpdate進(jìn)行時間同步的方法實現(xiàn),文中有相關(guān)的實現(xiàn)代碼,需要的朋友可以參考下2024-03-03通過shell腳本對mysql的增刪改查及my.cnf的配置
這篇文章主要介紹了通過shell腳本對mysql的增刪改查及my.cnf的配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07