欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

bash shell邏輯表達(dá)式的使用

 更新時間:2023年05月09日 14:31:31   作者:不識君  
bash shell邏輯表達(dá)式一般有3種寫法,本文就來介紹一下bash shell邏輯表達(dá)式的具體使用,具有一定的參考價值,感興趣的可以了解一下

前言

控制語句需要邏輯表達(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命令對于值的比較有固定的寫法(畢竟是個命令),主要分為3種:數(shù)值比較、字符串比較、文件類型檢查。

數(shù)字比較

數(shù)字的比較不能用== 、!=、<、>這些符號比較,有固定的參數(shù),就像上面的示例一樣,1==1,要寫成1 -eq 1,下面作個說明:

p.s. 如果記不住,可以用man test查看,下面是執(zhí)行man test中關(guān)于數(shù)值比較的說明部分:

       INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2

使用示例:test integer1 -eq integer2:比較integer1等于integer2,其它例同

字符串比較

字符串比較,可以用=或!=這些符號,另外還有一些非空檢查的選項,如下從(man test就看到了)復(fù)制過來的,加了點說明:

       -n STRING :字符串的長度大于0
the length of STRING is nonzero

       -z STRING:字符串的長度為0
the length of STRING is zero

上面的2項,可以對字符串是否為空字符串判斷,如果字符串為空(不是空字符串),無論是-n還是-z都是true

       STRING1 = STRING2
the strings are equal

       STRING1 != 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):

文件比較、類型/屬性檢查

下面就不過多解釋了,每個選項的描述很清楚了,也不需要刻意去記每個選項,還是那句話:man test,

       FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2

       FILE1 -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

因為test是個命令,所以邏輯與/或不能像其它語言一樣使用&&、||等符號,這幾個符號在shell中可是有特殊的意義,比如上面示例,用&&連接兩條命令,每1條執(zhí)行成功,才會執(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é)果:

[命令

注意是“[”,這是個命令,寫法一般是[ expression ],注意expression前后的空格,“[”可不是個符號, 是個命令,等同于test的命令,所以它的expression,和test的用法一樣。

這里重點是“[”是個命令,所以[,]這兩個符號和表達(dá)式之前要注意空格,另外,既然是和test一樣的命令,邏輯與不能&符號連接,也要用-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 ]]

雖然"[["只比"[",多了一個"[",但是含義卻是不一樣的,"["是一個命令,而"[["卻是一個關(guān)鍵字。

不過[[ expression ]]的功能卻比[ expression ]要強(qiáng)大不少,是[ expression ]的增強(qiáng)版。比如[[ expression ]]可以用&&作為邏輯與連接符而不支持用-a,同理邏輯或是||,但不支持-o。除此,其它的基本都兼容,另外[[ expression ]][ expression ],還有一些其它能力的支持,比如使用<,>等這些比較數(shù)字,但是因為"["是個命令,所以并不支持(需要轉(zhuǎn)義)。

所以,在感覺上來說使用[[ expression ]]更符合編程習(xí)慣,如下:

#! /bin/bash
#數(shù)字計算比較,
[[ 5 < $((3+3)) && 4 >3 ]]
echo "[[ 5 < 3+3 && 4 >3 ]] : $?"
#使用[],就不支持,運行到這里就報錯了
[ 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論