test 命令
2013-02-26 by dongnan
举个栗子
Linux 系统中如何判断一个文件是否为空?
使用 test 命令:
# 创建空文件
touch abc
# 测试文件是否为空
test -s abc
# 上一条命令的执行状态,0为真,非0为假!!!
echo $?
1
# 写入字符
echo 1 >> abc
# 再次测试非空文件
test -s abc
echo $?
0
在 shell 中,可以使用 echo $? 来查看命令的执行状态, 值0为真 ,值1(非0)为假。
命令帮助
NAME
   test - check file types and compare values
SYNOPSIS
   test EXPRESSION
   test
   [ EXPRESSION ]
   [ ]
   [ OPTION
DESCRIPTION
   Exit with the status determined by EXPRESSION.
    -e FILE
        FILE exists
    -n STRING
        the length of STRING is nonzero STRING equivalent to -n STRING
    -z STRING
        the length of STRING is zero
    -s FILE
        FILE exists and has a size greater than zero