2016-10-09 136 views
0

我想做一個監控腳本,並在特殊事件的情況下聯繫指定的人,下面的代碼中的第一個egrep工作正常,但第二個沒有工作,請幫助:grep不工作while循環

while true; do 
    tail -f /Testing/script/errors | egrep -o --line-buffer "Timeout" >> wch.txt; 
    cat /Testing/script/contacts.txt|egrep --line-buffer "FB" | cut -f2 -d ":" >> mail.txt; 
    sleep 5 
done 

contact.txt:

FB:[email protected] 谷歌:[email protected]

回答

0

有作爲--line-buffer沒有這樣的選擇,也許你的意思--line-buffered , 它是 GNU主義。

第二grep可以簡化爲:

grep --line-buffered 'FB' /Testing/script/contacts.txt | cut -f2 -d ":" >> mail.txt 

您不使用任何擴展的正則表達式模式,因此存在使用egrepgrep -E)沒有意義的。你也不需要cat,管道,grep以文件名作爲參數。

類似於第一grep可以寫成:

tail -f /Testing/script/errors | grep -o --line-buffered "Timeout" >> wch.txt 

所以你while結構:

while :; do 
    tail -f /Testing/script/errors | egrep -o --line-buffer "Timeout" >> wch.txt 
    grep --line-buffered 'FB' /Testing/script/contacts.txt | cut -f2 -d ":" >> mail.txt 
    sleep 5 
done 
+0

我照你所說的做了如下圖所示,它沒有工作: –

+0

而真 做 tail -f/Testing/script/errors | egrep -o --line-buffer「Timeout」>> wch.txt; grep -line-buffered'FB'/Testing/script/contacts.txt | cut -f2 -d「:」>> mail.txt sleep 5 done –

+0

第一個grep工作正常,但第二個仍然不工作也許它不是grep或什麼? –

0

-f國旗在第2線路阻塞循環。隨着文件增長,輸出將在/Testing/script/errors附加新行。

+0

即使我刪除它沒有工作 –