【发布时间】:2012-04-15 10:16:45
【问题描述】:
我有一个名为 diff.txt 的文件。我要检查是否为空。
我写了一个类似下面的 bash 脚本,但我无法让它工作。
if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm emtpy.txt
fi
【问题讨论】:
-
[ -s FILE ] 如果 FILE 存在且大小大于零,则为真。因此,如果“diff.txt”不为空,则会得到“empty.txt”。
-
PS:如果要查看实际的
diff调用,只需查看返回值:if diff foo.txt bar.txt; then echo 'No difference' -
可以否定测试:
if [ ! -s diff.txt ]; then echo "IS EMPTY";else echo "HAS SOMETHING";fi -
注意尾随的换行符。使用
$ cat diff.txt | hexdump -C检查文件
标签: linux bash unix file-handling is-empty