2017-08-08 84 views
0

我需要一個小腳本來搜索日誌文件中的字符串並計算行數。因爲這可能需要我一會兒,我還希望在控制檯中顯示「grep」的輸出。我的想法是這樣的:Bash腳本輸出到控制檯和文件

grep -irl "System out of memory" | tee /tmp/checkoom.tmp 
COUNT=(cat /tmp/checkoom.tmp |wc -l) 
echo $COUNT logs found. 

但它不工作。我沒有得到任何輸出,tmp文件被創建,但似乎是空的。

回答

0

你忘了提供起始目錄來回搜索:

grep -irl "System out of memory" <somewhere> | tee /tmp/checkoom.tmp 
0

這裏是在/ var/log/messages中得到USB記錄的一個班輪:

echo "$(grep -i '/lib/udev/usb' /var/log/messages > ~/yourFile ; cat ~/yourFile | wc -l) logs found" 

希望你覺得它有用

+0

是的,但我沒有得到從grep輸出... – Kjellson

0

腳本中有兩個錯誤:

  • grep的路徑
  • $()用於執行命令。這有分配變量COUNT時

更新腳本中添加:

#!/bin/bash 
grep -irl "System out of memory" <path to search> | tee /tmp/checkoom.tmp 
COUNT=$(cat /tmp/checkoom.tmp | wc -l) 
echo "$COUNT" logs found. 

而且你可以用grep的-F選項。 -F代表固定字符串

+0

奇怪的是,它仍然不工作。如果我使用沒有|的grep命令tee,evrything做得很好... – Kjellson

+0

這應該在正常情況下工作。嘗試使用「查找」。 Butgrep是正確的選擇:) 嘗試使用命令>> #find <搜索路徑> -type f -exec grep -irl「System out of memory」{} \; > /tmp/checkoom.tmp –