bash shell邏輯表達(dá)式的使用
前言
控制語(yǔ)句需要邏輯表達(dá)式進(jìn)行分支判斷,bash shell邏輯表達(dá)式一般有3種寫法:test expression、[ expression ] 、[[ expression ]]。
test
test命令的使用格式是:test expression。expression就是我們的邏輯表達(dá)式,如果值為true,執(zhí)行這條命令的返回值為0,false則非0。示例:
#! /bin/bash #1 == 1 test 1 -eq 1 echo "1 == 1 : $?" #1 == 2 test 1 -eq 2 echo "1 == 2 : $?"
執(zhí)行結(jié)果:

注意,test命令對(duì)于值的比較有固定的寫法(畢竟是個(gè)命令),主要分為3種:數(shù)值比較、字符串比較、文件類型檢查。
數(shù)字比較
數(shù)字的比較不能用== 、!=、<、>這些符號(hào)比較,有固定的參數(shù),就像上面的示例一樣,1==1,要寫成1 -eq 1,下面作個(gè)說明:
p.s. 如果記不住,可以用man test查看,下面是執(zhí)行man test中關(guān)于數(shù)值比較的說明部分:
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
使用示例:test integer1 -eq integer2:比較integer1等于integer2,其它例同
字符串比較
字符串比較,可以用=或!=這些符號(hào),另外還有一些非空檢查的選項(xiàng),如下從(man test就看到了)復(fù)制過來的,加了點(diǎn)說明:
-n STRING :字符串的長(zhǎng)度大于0
the length of STRING is nonzero-z STRING:字符串的長(zhǎng)度為0
the length of STRING is zero上面的2項(xiàng),可以對(duì)字符串是否為空字符串判斷,如果字符串為空(不是空字符串),無論是-n還是-z都是true
STRING1 = STRING2
the strings are equalSTRING1 != STRING2
the strings are not equal
示例:
#! /bin/bash str1="str1" test -n $str1;echo $? test -z $str2;echo $? test -n $str2;echo $? test x$str2 = x;echo $? test $str1 != "";echo $?
執(zhí)行結(jié)果(都是true):

文件比較、類型/屬性檢查
下面就不過多解釋了,每個(gè)選項(xiàng)的描述很清楚了,也不需要刻意去記每個(gè)選項(xiàng),還是那句話:man test,
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbersFILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2FILE1 -ot FILE2
FILE1 is older than FILE2-b FILE
FILE exists and is block special-c FILE
FILE exists and is character special-d FILE
FILE exists and is a directory-e FILE
FILE exists-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID-G FILE
FILE exists and is owned by the effective group ID-h FILE
FILE exists and is a symbolic link (same as -L)-k FILE
FILE exists and has its sticky bit set-L FILE
FILE exists and is a symbolic link (same as -h)-O FILE
FILE exists and is owned by the effective user ID-p FILE
FILE exists and is a named pipe-r FILE
FILE exists and read permission is granted-s FILE
FILE exists and has a size greater than zero-S FILE
FILE exists and is a socket-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set-w FILE
FILE exists and write permission is granted-x FILE
FILE exists and execute (or search) permission is granted
示例:
#! /bin/bash #文件是否存在 test -e demo.sh echo "demo.sh exists: $?" test -e demo1.sh echo "demo1.sh exists: $?"
執(zhí)行結(jié)果:

邏輯與:-a,邏輯或:-o
因?yàn)閠est是個(gè)命令,所以邏輯與/或不能像其它語(yǔ)言一樣使用&&、||等符號(hào),這幾個(gè)符號(hào)在shell中可是有特殊的意義,比如上面示例,用&&連接兩條命令,每1條執(zhí)行成功,才會(huì)執(zhí)行下一條。
示例:
#! /bin/bash #1等于1并且1等于2 test 1 -eq 1 -a 1 -eq 2 echo "1 == 1 && 1 == 2: $?" #1等于1或者1等于2 test 1 -eq 1 -o 1 -eq 2 echo "1 == 1 || 1 == 2: $?"
執(zhí)行結(jié)果:

[命令
注意是“[”,這是個(gè)命令,寫法一般是[ expression ],注意expression前后的空格,“[”可不是個(gè)符號(hào), 是個(gè)命令,等同于test的命令,所以它的expression,和test的用法一樣。
這里重點(diǎn)是“[”是個(gè)命令,所以[,]這兩個(gè)符號(hào)和表達(dá)式之前要注意空格,另外,既然是和test一樣的命令,邏輯與不能&符號(hào)連接,也要用-a等連接:
#! /bin/bash #1等于1并且1等于2 [ 1 -eq 1 -a 1 -eq 1 ] echo "1 == 1 && 1 == 2: $?" #1等于1或者1等于2 [ 1 -eq 1 -o 1 -eq 1 ] echo "1 == 1 || 1 == 2: $?"
執(zhí)行結(jié)果:

[[ expression ]]
雖然"[["只比"[",多了一個(gè)"[",但是含義卻是不一樣的,"["是一個(gè)命令,而"[["卻是一個(gè)關(guān)鍵字。
不過[[ expression ]]的功能卻比[ expression ]要強(qiáng)大不少,是[ expression ]的增強(qiáng)版。比如[[ expression ]]可以用&&作為邏輯與連接符而不支持用-a,同理邏輯或是||,但不支持-o。除此,其它的基本都兼容,另外[[ expression ]]比[ expression ],還有一些其它能力的支持,比如使用<,>等這些比較數(shù)字,但是因?yàn)?quot;["是個(gè)命令,所以并不支持(需要轉(zhuǎn)義)。
所以,在感覺上來說使用[[ expression ]]更符合編程習(xí)慣,如下:
#! /bin/bash #數(shù)字計(jì)算比較, [[ 5 < $((3+3)) && 4 >3 ]] echo "[[ 5 < 3+3 && 4 >3 ]] : $?" #使用[],就不支持,運(yùn)行到這里就報(bào)錯(cuò)了 [ 5 < $((3+3)) && 4 >3 ]
執(zhí)行結(jié)果:

可以看到第7行代碼執(zhí)行不通過,所以用[]的寫法就該是這樣:[ 5 -lt $((3+3)) -a 4 -gt 3 ]
到此這篇關(guān)于bash shell邏輯表達(dá)式的使用的文章就介紹到這了,更多相關(guān)shell邏輯表達(dá)式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux Bash 提示符的一些騷操作(自定義 Bash 提示符)
這篇文章主要介紹了Linux Bash 提示符的一些騷操作,一些能讓你自定義 Bash 提示符的黑科技,需要的朋友可以參考下2017-07-07
Shell腳本中獲取命令運(yùn)行結(jié)果的實(shí)現(xiàn)
本文主要介紹了Shell腳本中獲取命令運(yùn)行結(jié)果的實(shí)現(xiàn),除了我們熟知的管道 | 和args,我們也可以通過獲取命令的運(yùn)行結(jié)果,本文就來介紹一下,感興趣的可以了解一下2023-10-10
檢測(cè)網(wǎng)站down掉后自動(dòng)發(fā)信的shell腳本代碼
腳本用途:檢測(cè)指定文件中的網(wǎng)站url,當(dāng)一個(gè)網(wǎng)站down掉后,自動(dòng)給指定的郵箱發(fā)信2013-02-02
shell腳本學(xué)習(xí)指南[三](Arnold?Robbins?&?Nelson?H.F.?Beebe著)
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[三](Arnold?Robbins?&?Nelson?H.F.?Beebe著),需要的朋友可以參考下2014-02-02
linux 檢測(cè)遠(yuǎn)程端口是否打開方法總結(jié)
這篇文章主要介紹了linux 檢測(cè)遠(yuǎn)程端口是否打開方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
通過Spring Shell 開發(fā) Java 命令行應(yīng)用
這篇文章主要介紹了通過Spring Shell 開發(fā) Java 命令行應(yīng)用的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11

