Shell编程之条件判断语句

Shell编程之条件判断语句

一、条件判断

Shell环境根据命令执行后的返回状态值(echo $?)来判断是否执行成功,当返回值为0表示成功或正确,返回值为非0值表示失败或异常。(补充:Linux判断依据在别的编程语言中是反过来的,如java假为0,真为1)

1、test命令

有两种方式

  • test 条件表达式
  • [ 条件表达式 ] [ ] 相当于test 中括号之间需要有空格

案例:0表示成立 其他值表示不成立

[root@VM-20-11-centos shell]# test 1=1
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

2、文件测试

  • test 选项 文件/目录路径 test 选项 $变量
  • [ 选项 文件/目录路径 ] [ 选项 $变量 ]

常用的操作符号有

-e 测试目录或文件是否存在(Exist)
-d 测试是否为目录(Directory)
-f 测试是否为文件(File)
-r 测试当前用户是否有权限读取(Read)
-w 测试当前用户是否有权限写入(Write)
-x 测试是否设置有可执行(Excute)权限。
-L 测试是否为符号链接

案例:

# 返回值为1代表不存在
[root@VM-20-11-centos shell]# [ -e wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为1代表不是目录
[root@VM-20-11-centos shell]# [ -d wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为0代表是文件
[root@VM-20-11-centos shell]# ls -alh
total 28K
drwxrwxrwx 2 root root 4.0K Jul  8 10:10 .
drwxr-xr-x 5 root root 4.0K Jul  5 10:46 ..
-rw-r--r-x 1 root root  341 Jul  8 09:47 auto_var.sh
-rw-r--r-x 1 root root   54 Jul  5 17:44 first_shell.sh
-rw-r--r-x 1 root root  146 Jul  8 10:03 test_dir.sh
-rw-r--r-x 1 root root   85 Jul  8 09:59 test_num.sh
-rw-r--r-x 1 root root  200 Jul  8 10:11 test_scores.sh
[root@VM-20-11-centos shell]# [ -f auto_var.sh ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

3、整数值比较

  • 格式:[ 整数变量1 操作符 整数变量2 ]

常用的操作符

-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于

案例

[root@VM-20-11-centos shell]# [ 1 -le 1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

4、字符串判断

格式

  • test "字符串1" == "字符串2"
  • [ "字符串1" == "字符串2" ]

案例:

[[ "字符串1" =~ "字符串2 ]] # 判断字符串1是否包含字符串2

test -z "字符串" test -z "$变量" [ -z "$变量" ] #判断字符串或变量是否为空

test -n "字符串" test -n "$变量" [ -n "$变量" ] #判断字符串或变量是否有字符串


[root@VM-20-11-centos shell]# [ "abc" == "123" ]
[root@VM-20-11-centos shell]# echo $?
1

[root@VM-20-11-centos shell]# [[ "abcdfre" =~ "bc" ]]
[root@VM-20-11-centos shell]# echo $?
0

[root@VM-20-11-centos shell]# [ -z "a" ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ -z "" ]
[root@VM-20-11-centos shell]# echo $?
0

[root@VM-20-11-centos shell]# [ -n ""]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ -n null ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

5、逻辑测试

  • 格式1:[ 表达式1 ] 操作符 [ 表达式2 ]
  • 格式2:命令1 操作符 命令2

常用操作符 -a或者&& 而且的意思

             -o或者 ll  或者的意思

              !: 代表取反

    [ 表达式1 ] && [ 表达式2 ] ;[ 表达式1 -a 表达式2 ] ;[[ 表达式1 && 表达式2 ]]
[root@VM-20-11-centos shell]# [ 1=1 ] && [ 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -a 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [[ 1=1 && 2=2 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
[root@VM-20-11-centos shell]# [[ 1=1 || 2=3 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -o "abc"=="bdc" ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
# 取反
[root@VM-20-11-centos shell]# [ ! 12=12 ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ 12=12 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

案例2

#!/bin/bash
str="yingtan"
if [[ $str == "yingtan" || $str == "鹰潭" ]];then 
    echo "鹰潭"
else
    echo "不是鹰潭"
fi

执行结果

[root@VM-20-11-centos shell]# sh test_str.sh
鹰潭
[root@VM-20-11-centos shell]#

posted @ 2024-07-08 14:21  HandsomeWu  阅读(3)  评论(0编辑  收藏  举报