2014-05-08 46 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