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