Bash中test命令的使用
這個(gè)命令在if條件句中用得很多。test命令后都會(huì)跟一個(gè)表達(dá)式,作為它的參數(shù)。它有兩種寫法:
test EXPRESSION [ EXPRESSION ]
test的執(zhí)行過(guò)程就是拿一個(gè)元素與另一個(gè)元素進(jìn)行比較。在網(wǎng)絡(luò)上找了一個(gè)很有意思的例子,用它來(lái)說(shuō)明一下test命令的使用:
test 1 -eq 2 && echo "true" || echo "false"
- 1:是用來(lái)作比較的第一個(gè)參數(shù)
- -eq:這是具體的比較方法
- 2:這是用來(lái)比較的第二個(gè)參數(shù)
如果比較的結(jié)果是true,打印true,否則打印false
我們可以通過(guò)$?拿到test的結(jié)果。如果表達(dá)式的值是false,則$?的值是1,否則就是0。
上面的語(yǔ)句與下同的表達(dá)是一樣的:
[ 1 -eq 2 ] && echo "true" || echo "false"
整型相關(guān)的表達(dá)式用到的兩個(gè)數(shù)據(jù)的比較方法如下:
- -eq:等于 (equal to)
- -ne:等于 (not equal to)
- -gt:大于 (greater than)
- -ge:大于或等于(greater than or equal to)
- -lt:小于 (less than)
- -le:小于或等于(less than or equal to)
#!/usr/bin/env bash test 1 -eq 2 && echo "true" || echo "false" test 1 -ne 2 && echo "true" || echo "false" test 1 -gt 2 && echo "true" || echo "false" test 1 -ge 2 && echo "true" || echo "false" test 1 -lt 2 && echo "true" || echo "false" test 1 -le 2 && echo "true" || echo "false" [ 1 -eq 2 ] && echo "true" || echo "false" [ 1 -ne 2 ] && echo "true" || echo "false" [ 1 -gt 2 ] && echo "true" || echo "false" [ 1 -ge 2 ] && echo "true" || echo "false" [ 1 -lt 2 ] && echo "true" || echo "false" [ 1 -le 2 ] && echo "true" || echo "false"
shell提供了字符串比較相關(guān)的表達(dá)式:
- -n <string>: 字符串長(zhǎng)度不為零
- -z <string>: 字符串長(zhǎng)度為零
- <string>: 字符串值非零,與 -n <string>等價(jià)
- <string1> = <string2>: 兩個(gè)字符串是否相等
- <string1> != <string2>: 兩個(gè)字符串是否不相等
針對(duì)字符串,shell提供了這些方便使用的表達(dá)式。比如說(shuō):-n <string>這個(gè)表達(dá)式就是將字符串長(zhǎng)度與0作比較。其他依次類推。
test -n string1 && echo "true" || echo "false" test -z string1 && echo "true" || echo "false" test string1 && echo "true" || echo "false" test string1=string2 && echo "true" || echo "false" test string1!=string2 && echo "true" || echo "false" [ -n string1 ] && echo "true" || echo "false" [ -z string1 ] && echo "true" || echo "false" [ string1 ] && echo "true" || echo "false" [ string1=string2 ] && echo "true" || echo "false" [ string1!=string2 ] && echo "true" || echo "false"
shell也提供了與文件相關(guān)的比較表達(dá)式:
- <file1> -ef <file2>: 兩個(gè)文件是否有相似的device和inode編號(hào)(這些概念在Linux相關(guān)的知識(shí)可以了解到。)
- <file1> -nt <file2>:通過(guò)比較文件的修改日期,判斷file1是否比f(wàn)ile2要新。(nt :newer than)
- <file1> -ot <file2>:通過(guò)比較文件的修改日期,判斷file1是否比f(wàn)ile2要舊。(ot :older than)
- -e <file>:文件是否存在(exists)
- -f <file>:文件存在且是一個(gè)常規(guī)文件(file)
- -d <file>:文件存在且是一個(gè)目錄(directory)
- -r <file>:文件存在且有讀權(quán)限(read)
- -w <file>:文件存在且有寫權(quán)限(write)
- -x <file>:文件存在且有執(zhí)行權(quán)限 (execute)
- -s <file>:文件存在且文件大小大于0(size)
- -S <file>:文件存在且文件是一個(gè)socket
- -O <file>:文件存在且文件所有者是有效的用戶ID(owner)
- -G <file>:文件存在且文件所有者是有效的用戶組ID(group)
- -h <file>:文件存在且是一個(gè)符號(hào)連接文件(hard)
- -L <file>:文件存在且是一個(gè)符號(hào)連接文件(link)
- -b <file>:文件存在且是一個(gè)特殊塊文件(block)
- -c <file>:文件存在且是一個(gè)特殊字符文件(character)
#!/usr/bin/env bash test -e /bin/bash && echo $? || echo $? test -f /bin/bash && echo $? || echo $? test -d /bin/bash && echo $? || echo $? test -r /bin/bash && echo $? || echo $? test -w /bin/bash && echo $? || echo $? test -x /bin/bash && echo $? || echo $? test -s /bin/bash && echo $? || echo $? test -S /bin/bash && echo $? || echo $? test -O /bin/bash && echo $? || echo $? test -G /bin/bash && echo $? || echo $? test -h /bin/bash && echo $? || echo $? test -L /bin/bash && echo $? || echo $? test -b /bin/bash && echo $? || echo $? test -c /bin/bash && echo $? || echo $? #!/usr/bin/env bash [ -e /bin/bash ] && echo $? || echo $? [ -f /bin/bash ] && echo $? || echo $? [ -d /bin/bash ] && echo $? || echo $? [ -r /bin/bash ] && echo $? || echo $? [ -w /bin/bash ] && echo $? || echo $? [ -x /bin/bash ] && echo $? || echo $? [ -s /bin/bash ] && echo $? || echo $? [ -S /bin/bash ] && echo $? || echo $? [ -O /bin/bash ] && echo $? || echo $? [ -G /bin/bash ] && echo $? || echo $? [ -h /bin/bash ] && echo $? || echo $? [ -L /bin/bash ] && echo $? || echo $? [ -b /bin/bash ] && echo $? || echo $? [ -c /bin/bash ] && echo $? || echo $?
shell提供了上面這些方便的表達(dá)式,我們就少做了很多功夫。
所以,現(xiàn)在看來(lái)test很簡(jiǎn)單,但是很有用。因?yàn)閟hell腳本里會(huì)出現(xiàn)很多條件語(yǔ)句,test會(huì)用到很多。
到此這篇關(guān)于Bash中test命令的使用的文章就介紹到這了,更多相關(guān)Bash test命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PowerShell實(shí)現(xiàn)簡(jiǎn)單的grep功能
下面的PS腳本針對(duì)目錄和文件進(jìn)行了區(qū)分,借用Select-String命令,實(shí)現(xiàn)了內(nèi)容查找,并顯示查找到的文件和匹配內(nèi)容所在行號(hào)。感興趣的朋友一起看看吧2017-10-10linux中編寫自己的并發(fā)隊(duì)列類(Queue 并發(fā)阻塞隊(duì)列)
這篇文章主要介紹了linux中編寫并發(fā)隊(duì)列類,功能有:并發(fā)阻塞隊(duì)列、有超時(shí)限制、有大小限制2013-12-12linux下保留文件系統(tǒng)下剩余指定數(shù)目文件的shell腳本
本文介紹下,用于保留文件系統(tǒng)下剩余指定數(shù)量的文件的一個(gè)shell腳本,感興趣的朋友可以參考下2013-11-11Linux Shell腳本系列教程(七):腳本調(diào)試
這篇文章主要介紹了Linux Shell腳本系列教程(七):腳本調(diào)試,本文講解了Bash內(nèi)建調(diào)試功能和自定義調(diào)試功能等內(nèi)容,需要的朋友可以參考下2015-06-06