網站首頁 編程語言 正文
這個命令在if條件句中用得很多。test命令后都會跟一個表達式,作為它的參數。它有兩種寫法:
test EXPRESSION
[ EXPRESSION ]
test的執行過程就是拿一個元素與另一個元素進行比較。在網絡上找了一個很有意思的例子,用它來說明一下test命令的使用:
test 1 -eq 2 && echo "true" || echo "false"
- 1:是用來作比較的第一個參數
- -eq:這是具體的比較方法
- 2:這是用來比較的第二個參數
如果比較的結果是true,打印true,否則打印false
我們可以通過$?拿到test的結果。如果表達式的值是false,則$?的值是1,否則就是0。
上面的語句與下同的表達是一樣的:
[ 1 -eq 2 ] && echo "true" || echo "false"
整型相關的表達式用到的兩個數據的比較方法如下:
- -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提供了字符串比較相關的表達式:
- -n <string>: 字符串長度不為零
- -z <string>: 字符串長度為零
- <string>: 字符串值非零,與 -n <string>等價
- <string1> = <string2>: 兩個字符串是否相等
- <string1> != <string2>: 兩個字符串是否不相等
針對字符串,shell提供了這些方便使用的表達式。比如說:-n <string>這個表達式就是將字符串長度與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也提供了與文件相關的比較表達式:
- <file1> -ef <file2>: 兩個文件是否有相似的device和inode編號(這些概念在Linux相關的知識可以了解到。)
- <file1> -nt <file2>:通過比較文件的修改日期,判斷file1是否比file2要新。(nt :newer than)
- <file1> -ot <file2>:通過比較文件的修改日期,判斷file1是否比file2要舊。(ot :older than)
- -e <file>:文件是否存在(exists)
- -f <file>:文件存在且是一個常規文件(file)
- -d <file>:文件存在且是一個目錄(directory)
- -r <file>:文件存在且有讀權限(read)
- -w <file>:文件存在且有寫權限(write)
- -x <file>:文件存在且有執行權限 (execute)
- -s <file>:文件存在且文件大小大于0(size)
- -S <file>:文件存在且文件是一個socket
- -O <file>:文件存在且文件所有者是有效的用戶ID(owner)
- -G <file>:文件存在且文件所有者是有效的用戶組ID(group)
- -h <file>:文件存在且是一個符號連接文件(hard)
- -L <file>:文件存在且是一個符號連接文件(link)
- -b <file>:文件存在且是一個特殊塊文件(block)
- -c <file>:文件存在且是一個特殊字符文件(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提供了上面這些方便的表達式,我們就少做了很多功夫。
所以,現在看來test很簡單,但是很有用。因為shell腳本里會出現很多條件語句,test會用到很多。
原文鏈接:https://blog.csdn.net/weixin_40763897/article/details/128299545
相關推薦
- 2022-05-04 C#設計模式之單例模式_C#教程
- 2023-07-24 vxe-grid實現 二維數據聯動
- 2022-07-02 如何對numpy?矩陣進行通道間求均值_python
- 2022-09-22 string類的模擬實現
- 2022-10-01 Pycharm安裝scrapy及初始化爬蟲項目的完整步驟_python
- 2022-08-12 Go單元測試對GORM進行Mock測試_Golang
- 2022-03-29 C#算法之兩數之和_C#教程
- 2022-04-15 MAUI使用Maui.Graphics.Controls繪制控件詳解_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支