2017-08-08 88 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 –