shell编程-shell条件测试

  1. 条件测试格式
    1. test 条件表达式
    2. [ 条件表达式 ]
    3. [[ 条件表达式 ]]
  2. 文件测试
  3. 数值比较
  4. 字符串比较

条件测试格式

test 条件表达式

test 完全等同于 [ ]

test -d /home/test
[ -d /home/test ]

[ 条件表达式 ]

注意: [ 后面必须空格,] 前面必须空格,否则语法错误!!!

[ 1 -le 5 ]
  • 使用-a和-o表示逻辑与和逻辑或
  • 在[ ]中==是字符匹配
    [root@iZwz9f92w7soch5m251ghgZ test]# name="Milton"
    [root@iZwz9f92w7soch5m251ghgZ test]# [ "$name" == Milton  ];echo $?
    0
    [root@iZwz9f92w7soch5m251ghgZ test]# [ "$name" == M*  ];echo $?
    1

[[ 条件表达式 ]]

注意: [[ 后面必须空格,]] 前面必须空格,否则语法错误!!!

[[ 1 < 5 ]]
  • [[ ]]使用&&和||来表示逻辑与和逻辑或
  • 在[[ ]]中== 和 != 是模式匹配
  • [[ ]]支持用=~进行正则匹配
    [root@iZwz9f92w7soch5m251ghgZ test]# name="Milton"
    [root@iZwz9f92w7soch5m251ghgZ test]# [[ "$name" == M*  ]];echo $?
    0
    [root@iZwz9f92w7soch5m251ghgZ test]# [[ "$name" =~ M  ]];echo $?
    0
    [root@iZwz9f92w7soch5m251ghgZ test]# [[ "$name" =~ M.*  ]];echo $?
    0

更多表达式 EXPRESSION 用法:
man test

文件测试

常见文件测试选项如下:

  • -d FILE: FILE exists and is a directory
  • -e FILE: FILE exists
  • -f FILE: FILE exists and is a regular file
[root@iZwz9f92w7soch5m251ghgZ test]# pwd
/root/test
[root@iZwz9f92w7soch5m251ghgZ test]# ll
total 4
-rw-r--r-- 1 root root   0 Aug 28 21:31 2022-08-28_file.txt
-rw-r--r-- 1 root root 103 Aug 28 22:32 ping.sh
[root@iZwz9f92w7soch5m251ghgZ test]# 
[root@iZwz9f92w7soch5m251ghgZ test]# test -d /root/test;echo $?
0
[root@iZwz9f92w7soch5m251ghgZ test]# test -f /root/test/ping.sh;echo $?
0
[root@iZwz9f92w7soch5m251ghgZ test]# test -f /root/test/pingping.sh;echo $?
1

数值比较

INTEGER1 -eq INTEGER2
      INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
      INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
      INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
      INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
      INTEGER1 is less than INTEGER2

INTEGER1 -ne INTEGER2
      INTEGER1 is not equal to INTEGER2
[root@iZwz9f92w7soch5m251ghgZ test]# num=10
[root@iZwz9f92w7soch5m251ghgZ test]# [ $num -eq 10 ] && echo "true" || echo "false"
true
[root@iZwz9f92w7soch5m251ghgZ test]# [ $num -ne 10 ] && echo "true" || echo "false"
false
[root@iZwz9f92w7soch5m251ghgZ test]# [ $num -gt 10 ] && echo "true" || echo "false"
false

字符串比较

-n STRING
      the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
      the length of STRING is zero

STRING1 = STRING2
      the strings are equal

STRING1 != STRING2
      the strings are not equal

提示:建议使用双引号!

[root@iZwz9f92w7soch5m251ghgZ test]# name="Milton"
[root@iZwz9f92w7soch5m251ghgZ test]# [ -z "$name" ] && echo "字符长度为0" || echo "字符长度不为0"
字符长度不为0
[root@iZwz9f92w7soch5m251ghgZ test]# [ -n "$name" ] && echo "字符长度不为0" || echo "字符长度为0"
字符长度不为0
[root@iZwz9f92w7soch5m251ghgZ test]# name="Milton"
[root@iZwz9f92w7soch5m251ghgZ test]# [ "$name" = "Milton" ]; echo $?
0
[root@iZwz9f92w7soch5m251ghgZ test]# [ "$name" = "Cherish" ]; echo $?
1
[root@iZwz9f92w7soch5m251ghgZ test]# [ "$name" != "Cherish" ]; echo $?
0
[root@iZwz9f92w7soch5m251ghgZ test]# num=10
[root@iZwz9f92w7soch5m251ghgZ test]# [[ "$num" =~ ^[0-9]+$ ]];echo $?
0
[root@iZwz9f92w7soch5m251ghgZ test]# num=1a
[root@iZwz9f92w7soch5m251ghgZ test]# [[ "$num" =~ ^[0-9]+$ ]];echo $?
1

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
My Show My Code