我是shell脚本的新手。我正在尝试编写一个脚本来检查日志文件是否有错误(错误字符串是硬编码的),我必须打印包含错误的行。我能够编写逻辑,但需要指针从用户输入读取文件。
感谢帮助谢谢。
从用户
中读取日志文件设置错误字符串
search="ERROR"
设置输出文件的路径
outfile="file1.txt"
find "$mydir" -type f -name "$filename" |while read file
do
RESULT=$(grep "$search" "$file")
if [[ ! -z $RESULT ]]
then
echo "Error(s) in $file: $RESULT" >> "$outfile"
fi
done
答案 0 :(得分:0)
我不确定你的意思"需要指针从用户输入读取文件"。我假设"指针"是脚本参数。
您可以使用此脚本:
#!/bin/bash
expected=outfile
for f in $@
do
if [ "$expected" = outfile ]; then
OUTFILE=$1
expected=search
elif [ "$expected" = search ]; then
SEARCH=$2
expected=files
elif [[ -f $f ]]; then
RESULT=`grep "$SEARCH" $f`
if [ -n "$RESULT" ]; then
echo -e "\n"
echo "Error(s) in "$f":"
echo $RESULT
echo -e "\n" >> $OUTFILE
echo "Error(s) in "$f":" >> $OUTFILE
echo $RESULT >> $OUTFILE
fi
fi
done
调用:
scriptname outfile search files
其中:
示例(我假设脚本的名称是searcherror,它位于系统路径中):
searcherror errorsfound.txt primary /var/log/*.log
searcherror moreerrors.txt "ORA-06502" file1.log /var/log/*.log ./mylogs/*