·在shell中,返回值为零表示成功(True),非零值为失败(False)。
·test express
[ express ]
在Shell script中,test express/[ express ]这个语法被大量地使用,它是个非常实用的指令。由於它的返回值即Exit Status,经常被运用在if/while/until的场合中。而在後面,我们也会大量运用到,在进入介绍if/while/until之前,有必要先解一下。
其返回值为0(True)或1(False),要看表述(express)的结果为何。
express格式
-b file 当档案存在并且属性是Block special(通常是/dev/xxx)时,返回True。
-c file 当档案存在并且属性是character special(通常是/dev/xxx)时,返回True。
-d file 当档案存在并且属性是目录时,返回True。
-e file 当档案存在时,返回True。
-f file 当档案存在并且是正常档案时,返回True。
-g file 当档案存在并且是set-group-id时,返回True。
-k file 当档案存在并且有"sticky" bit被设定时,返回True。
-L file 当档案存在并且是symbolic link时,返回True。
-p file 当档案存在并且是name pipe时,返回True。
-r file 当档案存在并且可读取时,返回True。
-s file 当档案存在并且档案大小大於零时,返回True。
-S file 当档案存在并且是socket时,返回True。
-t fd : 当fd被开启为terminal时,返回True。
-u file 当档案存在并且set-user-id bit被设定时,返回True。
-w file 当档案存在并且可写入时,返回True。
-x file 当档案存在并且可执行时,返回True。
-O file 当档案存在并且是被执行的user id所拥有时,返回True。
-G file 当档案存在并且是被执行的group id所拥有时,返回True。
file1 -nt file2 当file1比file2新时(根据修改时间),返回True。
file1 -ot file2 当file1比file2旧时(根据修改时间),返回True。
file1 -ef file2 当file1与file2有相同的device及inode number时,返回True。
-z string 当string的长度为零时,返回True。
-n string 当string的长度不为零时,返回True。
string1 = string2 string1与string2相等时,返回True。
string1 != string2 string1与string2不相等时,返回True。
! express express为False时,返回True。
expr1 -a expr2 expr1及expr2为True。
expr1 -o expr2 expr1或expr2其中之一为True。
arg1 OP arg2 OP是-eq[equal]、-ne[not-equal]、-lt[less-than]、-le[less-than-or-equal]、
-gt[greater-than]、-ge[greater-than-or-equal]的其中之一。
=======================================================
在Bash中,当错误发生在致命信号时,bash会返回128+signal number做为返回值。如果找不到命令,将会返回127。如果命令找到了,但该命令是不可执行的,将返回126。除此以外,Bash本身会返回最後一个指令的返回值。若是执行中发生错误,将会返回一个非零的值。
Fatal Signal : 128 + signo
Can't not find command : 127
Can't not execute : 126
Shell script successfully executed : return the last command exit status
Fatal during execution : return non-zero