linux 中条件判断关键字 -s 和 -z

发布时间 2023-10-06 19:45:52作者: 小鲨鱼2018

 

001、 -s file:文件存在而且文件不为空,则为真,否则为假

(base) [root@pc1 test01]# ls
a.txt  b.txt
(base) [root@pc1 test01]# ll -h    ## 两个侧式文件,a.txt不为空,b.txt则为空
total 4.0K
-rw-r--r--. 1 root root 10 Oct  6 19:40 a.txt
-rw-r--r--. 1 root root  0 Oct  6 19:40 b.txt
(base) [root@pc1 test01]# [ -s a.txt ]       ## a.txt存在,而且不为空,因此为真
(base) [root@pc1 test01]# echo $?
0
(base) [root@pc1 test01]# [ -s c.txt ]       ## c.txt不存在,因为为假
(base) [root@pc1 test01]# echo $?
1
(base) [root@pc1 test01]# [ -s b.txt ]       ## b.txt存在,但是b.txt为空,因此为假
(base) [root@pc1 test01]# echo $?
1

 

002、-z file