如题所述
第1个回答 2017-06-21
1
2
3
4
myfile=test.txt
if [ ! -s $myfile ]; then #如果文件存在且为空
echo "$myfile is empty."
fi
-s 代表存在且不为空
! 取反
2
3
4
myfile=test.txt
if [ ! -s $myfile ]; then #如果文件存在且为空
echo "$myfile is empty."
fi
-s 代表存在且不为空
! 取反
第2个回答 2017-06-21
在shell中通过test指令测试文件是否为空的示例脚本如下:
#! /bin/sh
if test -s file.txt; then
echo "hi"
else
echo "empty"
fi
#! /bin/sh
if test -s file.txt; then
echo "hi"
else
echo "empty"
fi
第3个回答 2017-06-20
在Linux中写脚本的时候,总免不了需要判断文件是否存在、文件内容是否为空等存在,而这些操作都可以用test 指令来实现本回答被提问者采纳