shell 數值比較和字符串比較


1. 數值比較

-eq        是否相等(equal)

-gt         是否大於(greater than)

-ge       是否大於等於(greater and equal than)

同理小於等於(le),小於(lt)   less

-ne        是否不等於(not equal)

1 #測試變量1是否大於等於2
2 if [ $1 -ge "2" ]; then
3     echo yes!
4 fi

 

2. 字符串比較

str1 =  str2           檢查字符串1是否等於字符串2

str1 !=  str2       檢查字符串1是否不等於字符串2

str1 > str2            檢查字符串1是否大於字符串2

str1  <  str2          檢查字符串1是否小於字符串2

-n str1                 檢查字符串的長度是否非0

-z str1                 檢查字符串的長度是否為0

舉例:

1 #判斷2個字符串是否相等
2 
3 if [ $name = "xiaoming" ];then
4     echo equal!
5 fi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM