網(wǎng)站首頁 編程語言 正文
這個(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
相關(guān)推薦
- 2022-04-18 python接口測試返回?cái)?shù)據(jù)為字典取值方式_python
- 2022-08-14 使用Composing?builds提升Android編譯速度_Android
- 2022-04-24 .NET?CORE?鑒權(quán)的實(shí)現(xiàn)示例_實(shí)用技巧
- 2022-06-06 Array.prototype.myjoin
- 2024-07-18 Spring Security概述快速入門
- 2022-03-07 C語言中的rand()和rand_r()詳解_C 語言
- 2022-08-15 前端寫代碼的時(shí)候,不滿足條件程序停止執(zhí)行下面的程序,并彈窗提示
- 2023-07-25 mongodb本地連接失敗解決方案
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支