2014-05-08 45 views
0

我已经创建了一个脚本,所以我可以搜索特定路径中的0kb文件,输出将被重定向到一个文本文件,然后通过电子邮件发送。该脚本的工作原理除了在电子邮件发送时或在脚本运行后查看时,fie都是空的。Bash脚本生成空文件

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

不知道我是否错过了任何帮助,将不胜感激。

感谢

+2

您已明确指示,'的/ tmp/output.txt'被__truncated__。你为什么期望内容在那里?你想达到什么目的? – devnull

+0

我猜''/ tmp/output.txt'应该在'$()'里面 – technosaurus

回答

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 protected]" 
fi 
# Remove the text file 
rm /tmp/output.txt 
+1

测试总是失败。尝试'find ...> /tmp/output.txt;如果[[-n /tmp/output.txt]] ...' –

+0

@WilliamPursell该测试将始终通过:)使用'-s' –

+0

也许你可以先发出'find'并使用一个条件来检查输出文件不是空的。 – devnull