日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Bash中test命令的使用_linux shell

作者:WongKyunban ? 更新時(shí)間: 2023-05-15 編程語言

這個(gè)命令在if條件句中用得很多。test命令后都會(huì)跟一個(gè)表達(dá)式,作為它的參數(shù)。它有兩種寫法:

test EXPRESSION
[ EXPRESSION ]

test的執(zhí)行過程就是拿一個(gè)元素與另一個(gè)元素進(jìn)行比較。在網(wǎng)絡(luò)上找了一個(gè)很有意思的例子,用它來說明一下test命令的使用:

test 1 -eq 2 && echo "true" || echo "false"
  • 1:是用來作比較的第一個(gè)參數(shù)
  • -eq:這是具體的比較方法
  • 2:這是用來比較的第二個(gè)參數(shù)

如果比較的結(jié)果是true,打印true,否則打印false

我們可以通過$?拿到test的結(jié)果。如果表達(dá)式的值是false,則$?的值是1,否則就是0。

上面的語句與下同的表達(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>: 字符串長度不為零
  • -z <string>: 字符串長度為零
  • <string>: 字符串值非零,與 -n <string>等價(jià)
  • <string1> = <string2>: 兩個(gè)字符串是否相等
  • <string1> != <string2>: 兩個(gè)字符串是否不相等

針對(duì)字符串,shell提供了這些方便使用的表達(dá)式。比如說:-n <string>這個(gè)表達(dá)式就是將字符串長度與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>:通過比較文件的修改日期,判斷file1是否比file2要新。(nt :newer than)
  • <file1> -ot <file2>:通過比較文件的修改日期,判斷file1是否比file2要舊。(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)在看來test很簡單,但是很有用。因?yàn)閟hell腳本里會(huì)出現(xiàn)很多條件語句,test會(huì)用到很多。

原文鏈接:https://blog.csdn.net/weixin_40763897/article/details/128299545

欄目分類
最近更新