Linux Shell脚本使用用户输入来搜索日志

时间:2019-01-22 20:47:13

标签: linux bash shell

我正在尝试编写一个脚本,该脚本从用户输入的关键字中搜索tomcat日志。我在使read命令正常工作时遇到问题。

#!/bin/bash

  TOM1=/ap/mvr/servers/tomcat1/logs/
  TOM2=/ap/mvr/servers/tomcat2/logs/

  find $TOM1 $TOM2 -type f -name "mvr-gateway.log" | while read file
  echo "What do you want to search for? " keyword
  read keyword
  do
     LOGERRORS=$(grep -B1 -A1 $keyword $file)
     if [[ ! -z $LOGERRORS ]]
     then
     echo $file
     else echo No Data Found
     fi
  done

1 个答案:

答案 0 :(得分:0)

尝试一下:

#!/bin/bash

TOM1=/ap/mvr/servers/tomcat1/logs/
TOM2=/ap/mvr/servers/tomcat2/logs/

read  -p "What do you want to search for?" search_term

grep -B1 -A1 ${search_term} $(find $TOM1 $TOM2 -type f -name "mvr-gateway.log") 

如前所述,首先询问您要搜索的术语。