33.3. 测试和比较: 另一种方法
优质
小牛编辑
125浏览
2023-12-01
对于测试,[[ ]]结构可能比[]更合适.同样地,算术比较可能用(( ))结构更有用.
1 a=8 2 3 # 下面所有的比较是等价的. 4 test "$a" -lt 16 && echo "yes, $a < 16" # "与列表" 5 /bin/test "$a" -lt 16 && echo "yes, $a < 16" 6 [ "$a" -lt 16 ] && echo "yes, $a < 16" 7 [[ $a -lt 16 ]] && echo "yes, $a < 16" # 在[[ ]]和(( ))中不必用引号引起变量 8 (( a < 16 )) && echo "yes, $a < 16" # 9 10 city="New York" 11 # 同样,下面的所有比较都是等价的. 12 test "$city" \< Paris && echo "Yes, Paris is greater than $city" # 产生 ASCII 顺序. 13 /bin/test "$city" \< Paris && echo "Yes, Paris is greater than $city" 14 [ "$city" \< Paris ] && echo "Yes, Paris is greater than $city" 15 [[ $city < Paris ]] && echo "Yes, Paris is greater than $city" # 不需要用引号引起$city. 16 17 # 多谢, S.C.