如何在Bash中检查文件是否为空? [英] How to check if a file is empty in Bash?
本文介绍了如何在Bash中检查文件是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为diff.txt的文件。想检查它是否为空。做过这样的事情却无法正常工作。
I have a file called diff.txt. Want to check if it is empty. Did something like this but couldn't get it working.
if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm emtpy.txt
fi
推荐答案
拼写错误很刺激,不是吗?检查你的空
的拼写,但是也可以试试这个:
Misspellings are irritating, aren't they? Check your spelling of empty
, but then also try this:
#!/bin/bash -e
if [ -s diff.txt ]
then
rm -f empty.txt
touch full.txt
else
rm -f full.txt
touch empty.txt
fi
我非常喜欢shell脚本,但是它的一个缺点是,当你拼错时,shell无法帮助你,而像你的C ++编译器这样的编译器可以帮助你。
I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you.
顺便提一下,我已经交换了 empty.txt
和 full.txt
的角色,正如@Matthias所建议的那样。
Notice incidentally that I have swapped the roles of empty.txt
and full.txt
, as @Matthias suggests.
这篇关于如何在Bash中检查文件是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文