shell(四)条件判断语句

92 阅读3分钟

条件判断语句在编程中使用是很频繁的,首先我们想到的就是if-else语句。但是这里要区分开,条件判断语句是指if括号里边的那个语句,不是指if-else,if-else是流程控制语句。

在shell编程中,他为我们提供了一个相对简单的语句test,如下所示:

# 声明一个变量
[root@VM_0_4_centos test]# t=hello-world
# 使用test语句来测试一下是否相等,一定要按照我这个格式写,否则会报错
[root@VM_0_4_centos test]# test $t = hello-world
# 输出结果,0为相等
[root@VM_0_4_centos test]# echo $?
0
# 再测试一下不相等的情况
[root@VM_0_4_centos test]# test $t = Hello-world
# 输出结果,1为不相等
[root@VM_0_4_centos test]# echo $?
1

 

这里的返回值和大家之前用的可能不太一样,在其他编程语言中,都是1为真,0为假,而在shell编程中恰好反过来了,为什么呢?shell是将其底层执行语句的结果返回回来了,因此是0为真,1为假。

 

到这,理论上shell的条件判断语句基本上就介绍完了,但是,我觉得吧,使用test语句还是有点麻烦,有没有简单一点的写法呢?还真有,使用[]判断,但是这里一定要注意,他是有自己的语法格式的

[ 代码 ]  # []左右一定要有空格。否则会报错。

我们将上方的示例改写一下:
[root@VM_0_4_centos test]# t=hello-world
[root@VM_0_4_centos test]# [ $t = Hello-world ]
[root@VM_0_4_centos test]# echo $?
1
[root@VM_0_4_centos test]# [ $t = hello-world ]
[root@VM_0_4_centos test]# echo $?
0

执行结果如上所示。条件判断嘛,肯定不能是只有等于和不等于,那我们接下来测试一下大于和小于的情况:

[root@VM_0_4_centos test]# t=5
[root@VM_0_4_centos test]# z=10
[root@VM_0_4_centos test]# [ $t > $z ]
[root@VM_0_4_centos test]# echo $?
0
[root@VM_0_4_centos test]# [ $t < $z ]
[root@VM_0_4_centos test]# echo $?
0

上边的两个判断结果都是返回0,难道5大于10?不可能啊,百度查了一下,shell编程中判断大于、小于不使用 > < 号。

号,文件输出重定向

<号,文件输入重定向

Shell中判断大小使用以下符号:

符号说明举例
-eq检测两个数是否相等,相等返回   true。[   aeqa -eq b ] 返回 false。
-ne检测两个数是否不相等,不相等返回   true。[   anea -ne b ] 返回 true。
-gt检测左边的数是否大于右边的,如果是,则返回   true。[   agta -gt b ] 返回 false。
-lt检测左边的数是否小于右边的,如果是,则返回   true。[   alta -lt b ] 返回 true。
-ge检测左边的数是否大于等于右边的,如果是,则返回   true。[   agea -ge b ] 返回 false。
-le检测左边的数是否小于等于右边的,如果是,则返回   true。[   alea -le b ] 返回 true。

 

我们测试一下:

[root@VM_0_4_centos test]# echo $t
5
[root@VM_0_4_centos test]# echo $z
10
[root@VM_0_4_centos test]# [ $t > $z ]
[root@VM_0_4_centos test]# echo $?
0
[root@VM_0_4_centos test]# [ $t -gt $z ]
[root@VM_0_4_centos test]# echo $?
1
[root@VM_0_4_centos test]# [ $t -lt $z ]
[root@VM_0_4_centos test]# echo $?
0

看到上方的代码,这次执行的结果就对了。

 

按照文件权限判断:

-r:有读的权限(read)

-w:有写的权限(write)

-x:有执行的权限(execute)

 

我们来测试一下:

# 查看当前目录下的所有文件以及子目录。
[root@VM_0_4_centos test]# ll
total 16
-rw-r--r-- 1 root root    0 Nov 11 16:25 10
-rw-r--r-- 1 root root   34 Nov  3 16:32 she
-rwxr-xr-x 1 root root   27 Nov  3 16:19 she.php
-rwxr-xr-x 1 root root   61 Nov  9 16:48 she.sh
drwxr-xr-x 2 root root 4096 Nov 11 16:54 test_son
# 判断she文件是否有可读权限,she有可读权限
[root@VM_0_4_centos test]# [ -r she ]
[root@VM_0_4_centos test]# echo $?
0
# 判断she文件是否有可执行权限,she没有可执行权限
[root@VM_0_4_centos test]# [ -x she ]
[root@VM_0_4_centos test]# echo $?
1
# 判断she文件是否有可写权限,she没有可写权限
[root@VM_0_4_centos test]# [ -w she ]
[root@VM_0_4_centos test]# echo $?
0

 

按照文件类型判断:

-e:文件存在(existence)

-f:文件存在并且是一个常规的文件(file)

-d:文件存在并且是一个目求(directory)

我们来测试一下:

# 查看当前目录下的所有文件以及子目录。
[root@VM_0_4_centos test]# ll
total 16
-rw-r--r-- 1 root root    0 Nov 11 16:25 10
-rw-r--r-- 1 root root   34 Nov  3 16:32 she
-rwxr-xr-x 1 root root   27 Nov  3 16:19 she.php
-rwxr-xr-x 1 root root   61 Nov  9 16:48 she.sh
drwxr-xr-x 2 root root 4096 Nov 11 16:54 test_son
# 判断文件 she是否存在
[root@VM_0_4_centos test]# [ -e she ]
[root@VM_0_4_centos test]# echo $?
0
# 判断文件 she是否是一个文件
[root@VM_0_4_centos test]# [ -f she ]
[root@VM_0_4_centos test]# echo $?
0
# 判断文件 she是否是一个目录
[root@VM_0_4_centos test]# [ -d she ]
[root@VM_0_4_centos test]# echo $?
1
# 判断目录test_son是否是一个目录
[root@VM_0_4_centos test]# [ -d test_son ]
[root@VM_0_4_centos test]# echo $?
0

 

上边我们测试的都是单条件判断语句。那如果我们需要同时判断多个条件我们应该怎么处理呢?

 

多条件判断:

&&表示前一条命令执行成功时,才执行后一条命令,‖表示上一条命令执行尖败后,才执行下一条命令

[root@VM_0_4_centos test]# [ -d test_son ] && echo true || echo false
true
[root@VM_0_4_centos test]# [ -f test_son ] && echo true || echo false
false

大概就是这个样子。

 

以上大概就是shell编程中条件判断语句的基本使用。

 

有好的建议,请在下方输入你的评论。