前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell 脚本中 if 各种条件判断的使用

shell 脚本中 if 各种条件判断的使用

原创
作者头像
特特
发布2022-12-08 00:34:28
2K0
发布2022-12-08 00:34:28
举报
文章被收录于专栏:特特的专栏特特的专栏

1. if 在shell中语法格式

1.1 if-elif-else语法格式

代码语言:shell
复制
if [ command ];then
elif [ command ];then
else
fi

1.2 if-else语法格式

代码语言:shell
复制
if [ command ];then
else
fi

1.3 if语法格式

代码语言:shell
复制
if [ command ];then
fi

2. 字符串运算符

代码语言:text
复制
 =	检测两个字符串是否相等,相等返回 true。	[ $a = $b ] 返回 false。
 !=	检测两个字符串是否不相等,不相等返回 true。	[ $a != $b ] 返回 true。
 -z	检测字符串长度是否为0,为0返回 true。	[ -z $a ] 返回 false。
 -n	检测字符串长度是否不为 0,不为 0 返回 true。	[ -n "$a" ] 返回 true。
 $	检测字符串是否不为空,不为空返回 true。	[ $a ] 返回 true。

示例:

代码语言:shell
复制
str1="小明"
str2="小红"
str3=""
if [ $str1 = "小明" ];then
    echo "${str1} 和 小明 是相等的"
fi
if [ $str1 != $str2 ];then
    echo "${str1} 和 ${str2} 是不相等的"
fi
if [ -z $str3 ];then
    echo "${str3} 是空的"
fi
if [ -n $str1 ];then
    echo "${str1} 不是空的"
fi
if [ $str1 ];then
    echo "${str1} 不是空的"
fi

运行结果:

代码语言:shell
复制
小明 和 小明 是相等的
小明 和 小红 是不相等的
 是空的
小明 不是空的
小明 不是空的

3. 关系运算符

代码语言:shell
复制
-eq	检测两个数是否相等,相等返回 true。	[ $a -eq $b ] 返回 false。
-ne	检测两个数是否不相等,不相等返回 true。	[ $a -ne $b ] 返回 true。
-gt	检测左边的数是否大于右边的,如果是,则返回 true。	[ $a -gt $b ] 返回 false。
-lt	检测左边的数是否小于右边的,如果是,则返回 true。	[ $a -lt $b ] 返回 true。
-ge	检测左边的数是否大于等于右边的,如果是,则返回 true。	[ $a -ge $b ] 返回 false。
-le	检测左边的数是否小于等于右边的,如果是,则返回 true。	[ $a -le $b ] 返回 true。

示例:

代码语言:shell
复制
a=10
b=20
c=10
if [ $a -eq $c ];then
 echo "${a} == ${c}"
fi
if [ $a -ne $b ];then
 echo "${a} != ${b}"
fi
if [ $b -gt $a ];then
 echo "${b} > ${a}"
fi
if [ $a -lt $b ];then
 echo "${a} < ${b}"
fi
if [ $b -ge $a ];then
 echo "${b} >= ${a}"
fi
if [ $a -le $b ];then
 echo "${a} <= ${b}"
fi

运行结果:

代码语言:shell
复制
10 == 10
10 != 20
20 > 10
10 < 20
20 >= 10
10 <= 20

4. 逻辑运算符

代码语言:shell
复制
!	非运算,表达式为 true 则返回 false,否则返回 true。	[ ! false ] 返回 true。
-o ||	或运算,有一个表达式为 true 则返回 true。	[ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a &&	与运算,两个表达式都为 true 才返回 true。	[ $a -lt 20 -a $b -gt 100 ] 返回 false。

示例:

代码语言:shell
复制
a=10
b=20
if [ $a != $b ];then
 echo "${a} != ${b}"
fi
if [ $a -lt 100 -a $b -gt 10 ];then
 echo "${a} < 100 并且 ${b} > 10"
fi
if [ $a -lt 100 -o $b -gt 10 ];then
 echo "${a} < 100 或者 ${b} > 10"
fi

运行结果:

代码语言:shell
复制
10 != 20
10 < 100 并且 20 > 10
10 < 100 或者 20 > 10

5. 文件运算符

代码语言:shell
复制
-d file	检测文件是否是目录,如果是,则返回 true。	[ -d $file ] 返回 false。
-f file	检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。	[ -f $file ] 返回 true。
-r file	检测文件是否可读,如果是,则返回 true。	[ -r $file ] 返回 true。
-w file	检测文件是否可写,如果是,则返回 true。	[ -w $file ] 返回 true。
-x file	检测文件是否可执行,如果是,则返回 true。	[ -x $file ] 返回 true。
-s file	检测文件是否为空(文件大小是否大于0),不为空返回 true。	[ -s $file ] 返回 true。
-e file	检测文件(包括目录)是否存在,如果是,则返回 true。	[ -e $file ] 返回 true。
-b file	检测文件是否是块设备文件,如果是,则返回 true。	[ -b $file ] 返回 false。
-c file	检测文件是否是字符设备文件,如果是,则返回 true。	[ -c $file ] 返回 false。
-g file	检测文件是否设置了 SGID 位,如果是,则返回 true。	[ -g $file ] 返回 false。
-k file	检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。	[ -k $file ] 返回 false。
-p file	检测文件是否是有名管道,如果是,则返回 true。	[ -p $file ] 返回 false。
-u file	检测文件是否设置了 SUID 位,如果是,则返回 true。	[ -u $file ] 返回 false。

示例:

代码语言:shell
复制
file=/Users/xx/Documents/study/shell/shell_if_boolean.sh
dir_file=/Users/xx/Documents/study/shell/
if [ -d $dir_file ];then
  echo "${dir_file} 是一个目录"
fi
if [ -f $file ];then
 echo "${file} 是一个普通文件"
fi
if [ -r $file ];then
 echo "${file} 文件可读"
fi
if [ -w $file ];then
 echo "${file} 文件可写"
fi
if [ -x $file ];then
 echo "${file} 文件可执行"
fi
if [ -e $file ];then
 echo "${file} 文件存在"
fi
if [ -s $file ];then
 echo "${file} 文件为空"
else
 echo "${file} 文件不为空"
fi

运行结果:

代码语言:shell
复制
/Users/xx/Documents/study/shell/ 是一个目录
/Users/xx/Documents/study/shell/shell_if_boolean.sh 是一个普通文件
/Users/xx/Documents/study/shell/shell_if_boolean.sh 文件可读
/Users/xx/Documents/study/shell/shell_if_boolean.sh 文件可写
/Users/xx/Documents/study/shell/shell_if_boolean.sh 文件可执行
/Users/xx/Documents/study/shell/shell_if_boolean.sh 文件存在
/Users/xx/Documents/study/shell/shell_if_boolean.sh 文件为空

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. if 在shell中语法格式
    • 1.1 if-elif-else语法格式
      • 1.2 if-else语法格式
        • 1.3 if语法格式
        • 2. 字符串运算符
        • 3. 关系运算符
        • 4. 逻辑运算符
        • 5. 文件运算符
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档