1. if 在shell中语法格式

1.1 if-elif-else语法格式

if [ command ];then

elif [ command ];then

else

fi

1.2 if-else语法格式

if [ command ];then

else

fi

1.3 if语法格式

if [ command ];then

fi

2. 字符串运算符

= 检测两个字符串是否相等,相等返回 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。

示例:

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

运行结果:

小明 和 小明 是相等的

小明 和 小红 是不相等的

是空的

小明 不是空的

小明 不是空的

3. 关系运算符

-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。

示例:

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

运行结果:

10 == 10

10 != 20

20 > 10

10 < 20

20 >= 10

10 <= 20

4. 逻辑运算符

! 非运算,表达式为 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。

示例:

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

运行结果:

10 != 20

10 < 100 并且 20 > 10

10 < 100 或者 20 > 10

5. 文件运算符

-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。

示例:

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

运行结果:

/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 文件为空

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: