Bash脚本生成空文件

时间:2014-05-08 02:38:51

标签: linux bash

我创建了一个脚本,因此我可以在某个路径中搜索0kb文件,输出将被重定向到文本文件然后通过电子邮件发送。该脚本可以正常工作,除非fie在通过电子邮件发送时或在脚本运行后查看时都是空的。

if [[ -n $(find /path/to/files/ -type f -empty -mmin +2) ]] then
> /tmp/output.txt ; mail -s "subject" -a /tmp/output.txt "email@address"
rm /tmp/ftr_output.txt
fi   

不确定我是否遗漏了某些东西,所以任何帮助都会受到赞赏。

谢谢

1 个答案:

答案 0 :(得分:3)

我猜你想要这样的东西:

# Save find results in a text file
find /path/to/files/ -type f -empty -mmin +2 > /tmp/output.txt

# Check that the text file isn't empty (meaning no results from find)
if [ -s /tmp/output.txt ]
then
    # Send the text file along with an email
    mail -s "subject" -a /tmp/output.txt "email@address"
fi
# Remove the text file
rm /tmp/output.txt