我正在尝试编写一个脚本,该脚本从用户输入的关键字搜索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
最佳答案
尝试这个:
#!/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")
如前所述,首先询问您要搜索的术语。
关于linux - Linux Shell脚本使用用户输入来搜索日志,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54316206/