shell腳本之六:shell腳本的條件測試與比較

来源:https://www.cnblogs.com/cuiyongchao007/archive/2020/04/28/12798128.html
-Advertisement-
Play Games

六、shell腳本的條件測試與比較 (一)條件表達式的常見語法 1、條件表達式6種寫法(if,while) 語法1:test 語法2:[ ] 中括弧兩端必須要有空格 語法3:[[]] 兩端必須要有空格 語法4:((測試表達式)) 兩端必不需要空格 語法5:(命令表達式) 語法6: 實際應用展示 ①[ ...


六、shell腳本的條件測試與比較

(一)條件表達式的常見語法

1、條件表達式6種寫法(if,while)

語法1:test<測試表達式>

語法2:[ <測試表達式>] #中括弧兩端必須要有空格

語法3:[[<測試表達式>]] #兩端必須要有空格

語法4:((測試表達式)) #兩端必不需要空格

語法5:(命令表達式)

語法6:命令表達式

實際應用展示

①[]條件表達式
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc/host1 ] && echo 0 || echo 1
1
②test條件表達式:test和[]功能相同
[root@centos6-kvm3 scripts]# test -e /etc/host1  && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# test -e /etc/hosts  && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# # [] == test
③[[]]雙中括弧條件表達式,兩邊需要空格
[root@centos6-kvm3 scripts]# [[ -e /etc/host1 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [[ -e /etc/hosts ]] && echo 0 || echo 1
0
④雙括弧條件表達式,兩邊不需要空格
[root@centos6-kvm3 scripts]# (( -e /etc/hosts )) && echo 0 || echo 1
-bash: ((: -e /etc/hosts : division by 0 (error token is "/hosts ")
1
⑤雙括弧一般用於計算:
[root@centos6-kvm3 scripts]# ((3>5)) && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# ((3<5)) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# #(())用於計算
⑥expr 表達式:使用括弧
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + a &>/dev/null) && echo 0 || echo 1
1
⑦expr 表達式:使用反引號
[root@centos6-kvm3 scripts]# `expr 1 + a &>/dev/null` && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# `expr 1 + 2 &>/dev/null` && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# 

(二)條件表達式的編輯語法:

1、[<測試表達式>] && 命令1 ||命令2

如果前面表達式成功,那麼執行命令1,否則執行命令2

if [ <測試表達式>]
then
   命令1
else
   命令2
fi

2、多命令情況

當命令很多的時候,我們可以使用大括弧把所有命令括起來。如下:

[ <測試表達式> ] && {

命令1

命令2

}||{

命令3

命令4

}

3、只保留執行成功的

[ <測試表達式>] &&{

命令1

命令2

命令3

}

4、只保留執行失敗的

[<測試表達式>] || {

命令1

命令2

命令3

}

(三)文件測試表達式

man test

-d:文件為目錄且存在
[root@centos6-kvm3 scripts]# [ -d /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [ -d /etc ] && echo 0 || echo 1
0
-f:判斷為文件且存在
[root@centos6-kvm3 scripts]# [ -f /etc/hosts ] && echo 0 || echo 1
0
-e:判斷存在,為目錄或者文件
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc ] && echo 0 || echo 1
0
-r:判斷文件為可讀:
[root@centos6-kvm3 scripts]# [ -r /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# ll /etc/hosts
-rw-r--r--. 2 root root 352 Nov 19  2018 /etc/hosts
-x:判斷文件為可執行:
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# chmod +x /etc/hosts
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
0
-s:判斷文件大小不為0:
[root@centos6-kvm3 scripts]# [ -s /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# touch oldboy.log
[root@centos6-kvm3 scripts]# [ -s oldboy.log ] && echo 0 || echo 1
1
應用示例:crond
[root@centos6-kvm3 scripts]# cat /etc/init.d/crond 

(四)字元串測試表達式的常見功能說明

n:not zero ,[-n "字元串" ] 字元串長度不為0,表達式為真。
z:zero,[-z "字元串" ] 字元串長度為0,表達式為真。
["字元串1"==“字元串2”] 兩個字元串相同為真。
[“字元串1”!=“字元串2”] 兩個字元串不相同為真。
註意:
1、字元串就用雙引號。
2、等號可以用一個或者兩個。
3、等號兩端必須要有空格。

[root@centos6-kvm3 scripts]# [ -n "oldboy" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ -z "oldboy" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# char="oldboy"
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# unset char
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd"=="ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" == "ff" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd" == "dd" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "dd" ] && echo 1 || echo 0
0
實例應用:
cat /etc/init.d/crond 
cat /etc/init.d/network 

實例:

[root@centos6-kvm3 scripts]# cat select1.sh 
#!/bin/bash
cat << EOF
1.install lamp
2.install lnmp
3.exit
EOF
read -p "請輸入一個序號:" num
[ -z "$num" ] && exit 1 #判斷內容是否為空
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
   echo "usage:$0{1|2|3}"
   exit 1
fi

if [ $num -eq 1 ]
then
   echo "install lamp..."
elif [ $num -eq 2 ]
then
   echo "install lnmp ..."
elif [ $num -eq 3 ]
then
   echo "bye..."
   exit
else
   echo "usage:$0{1|2|3}"
   exit 1
fi

(五)整數測試表達式

在[]及test中使用的比較表達式 在(())和[[]]中使用的比較符號 說明
-eq ==或者= 等於equal
-ne != 不等於not equal
-gt > 大於greater then
-ge >= 大於等於greater equal
-lt < 小於 less then
-le <= 小於等於 less equal

實例

[root@centos6-kvm3 ~]# [ 2 -eq 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -gt 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -lt 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 > 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 \> 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 > 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -gt 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -lt 3 ]] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# (( 2 -lt 3 )) && echo 0 || echo 1
-bash: ((: 2 -lt 3 : syntax error in expression (error token is "3 ")
1
[root@centos6-kvm3 ~]# (( 2 > 3 )) && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# (( 2 < 3 )) && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# test 2 -gt 3 && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# test 2 -lt 3 && echo 0 || echo 1
0
總結:
1、雙中括弧中使用 字母表達式。
2、雙括弧中不適合字母表達式,只適合符號表達式。
3、test表達式只適合符號表達式。

(六)測試題:使用read的交互方式,來比較兩個整數的大小。

[root@centos6-kvm3 scripts]# cat test3.sh
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
   echo "請輸入兩個整數。"
   exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
   echo "請輸入兩個整數。"
   exit 2
}
[ $a -lt $b ] && {
   echo "$a小於$b."
   exit 0
}
[ $a -gt $b ] && {
   echo "$a大於$b."
   exit 0
}

[ $a -eq $b ] && {
   echo "$a等於$b."
   exit 0
}

================
使用if語句:
[root@centos6-kvm3 scripts]# cat test4.sh 
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
   echo "請輸入兩個整數。"
   exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
   echo "請輸入兩個整數。"
   exit 2
}
if [ $a -lt $b ]
then
   echo "$a小於$b."
elif [ $a -gt $b ]
then
   echo "$a大於$b."
else 
   echo "$a等於$b."
fi
[root@centos6-kvm3 scripts]# 

(七)邏輯測試表達式

在[]和test中使用操作符 在[[]]和(())中使用操作符 說明
-a && and 與
-o || or 或
! ! not 非

實例:

[root@centos6-kvm3 scripts]# [ 1 -eq 1 -a -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -a -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -o -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] -o [ -f /etc/hosts ] && echo 1 || echo 0
-bash: [: too many arguments
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] || [ -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] && [ -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 || -f /etc/hosts ]] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 && -f /etc/hosts ]] && echo 1 || echo 0
0

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.前端輸出後端返回的html內容,例如後端ViewBag.PayQrCode = “<img src="abc/a.jpg"”/> 則前端輸出方式為: @Html.Raw(ViewBag.PayQrCode) 2.EF發佈時需註意伺服器版本,修改Entities.edmx文件,用xml方式打開,例 ...
  • //調用ResXResourceReader類,需要引用System.Windows.Forms.dll,下同 ResXResourceReader resxReader = new ResXResourceReader(@"資源文件路徑"); IDictionaryEnumerator dict ...
  • 昨天發現線上試跑期的一個程式掛了,平時都跑的好好的,查了下日誌是因為昨天運營跑了一家美妝top級淘品牌店,會員量近千萬,一下子就把128G的記憶體給爆了,當時並行跑了二個任務,沒轍先速寫一段代碼限流,後面再做進一步優化。 一: 背景 1. 背景介紹 因為是自己寫的代碼,所以我知道問題出現在哪裡,如果大 ...
  • 1. 什麼是Expression Web Expression Studio是微軟在2007年推出的一套針對設計師的套件,其中包含專業的設計工具和新技術,可以彈性且自由地將設計方案轉為實際——無論設計的是標準的網站、擁有豐富用戶經驗的桌面應用,或是管理數字資產和內容。它包含以下部分: Express ...
  • errorloc和errorloc302都是同樣的效果,都是以臨時重定向到指定的url上;這裡還需要註意一點,這兩種方式都是跳轉前請求的方法是什麼,跳轉對應url也是同樣的方法;這樣一來對於其他非GET方法請求出現403錯誤碼的時候,對應的錯誤頁就無法正常處理(通常只允許GET方法去請求別的URL... ...
  • 九、case結構條件句應用實踐 (一)case語法結構 case結構條件句相當於多分支if條件語句,但是它比這些條件句看起來更規範工整,常被用於實現系統服務腳本等應用場景中。 case語句的語法結構: (二)實例,case應用: (三)實例,不同的分支字體顏色不同: 當用戶輸入對應的數字選擇水果的時 ...
  • 八、函數知識與實踐 (一)shell函數語法 1、函數的表示方式 | 第一種語法 | 第二種語法 | 第三種語法 | | | | | | function 函數名(){ } | function 函數名 {} | 函數名() { } | 2、實例:函數的寫法 3、實例:檢測web網站是否正常 wge ...
  • 七、if結構條件句知識與實踐 (一)if條件句單雙分支語法 (二)if條件句多分支語句 1、語句的結構 2、實例:判斷目錄是否存在 3、實例:判斷伺服器記憶體大小 開發shell腳本判斷記憶體是否充足,如果小於100,提示不足,如果大於100提示充足。 [root@centos6 kvm3 script ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...