我编写了一个脚本来检查最后10分钟的httpd错误日志,但是我得到了一个错误,它保持循环:问题是,在日志文件结束之前,脚本的输出是一个错误循环,我试着通过运行cmd date -- date =‘-10 min获得日期- 10分钟,然后逐行解析日志文件,然后检查日志文件中的每一行是否大于或等于日期的小时和分钟--这里的10分钟是输出的一部分:
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found当我尝试调试它时,这里是问题的一部分,它在日志文件的每一行中都会重复:
+ IFS=
+ read -r line
+ errorBool=0
+ [[ [Sun Apr 28 03:52:39.791442 2019] [autoindex:error] [pid 15012]
[client 127.0.0.1:49054] AH01276: Cannot serve directory /var/www/html/:
No matching DirectoryIndex (index.html,index.php) found, and server-
generated directory index forbidden by Options directive == *:error* ]]
++ awk -F : '{printf $1}'
++ '[Sun' Apr 28 03:52:39.791442 '2019]' '[autoindex:error]' '[pid'
'15012]' '[client' '127.0.0.1:49054]' AH01276: Cannot serve directory
/var/www/html/: No matching DirectoryIndex '(index.html,index.php)' found,
and server-generated directory index forbidden by Options directive
test2.sh: line 26: [Sun: command not found
++ awk '{printf $4}'
+ '[' -ge 12 ']'
test2.sh: line 26: [: -ge: unary operator expected以下是代码:
#!/bin/bash
#SCRIPT TO CHECK THE ERROR LOGS IN THE LAST 10 MINS
#VARIABLES
#NUMBER OF ERROR LOGS
errorCount=0
DATE_10_MIN=$(date --date='-10min' | awk '{print $4}' )
DATE_10_MIN_HOURS=$(date --date='-10min' | awk '{print $4}' | awk -F :
'{print $1} ')
DATE_10_MIN_MIN=$(date --date='-10min' | awk '{print $4}' | awk -F :
'{print $2} ')
#_______________________#
while IFS= read -r line ; do
#BOOLEAN TO CHECK IF THE LINE HAVE THE EXPRESSION
errorBool=0
#if [[ $($line | awk '{print $4 }' | cut -c-8) -gt $DATE_10_MIN ]] ;
then
if [[ $line == *:error* ]] ; then
if [ [ $($line | awk '{print $4}' | awk -F : '{print $1}' )
-ge $DATE_10_MIN_HOURS ] && [ $($line | awk '{print $4}' | awk -F :
'{print $2}') -ge $DATE_10_MIN_MIN ] ]; then
errorBool=1
(( errorCount++ ))
echo $line
fi
fi
done < /var/log/httpd/error_log
echo "There were $errorCount error logs in the last 10 mins "发布于 2019-05-06 12:38:27
我看到awk -F :之后的换行符,我会写:
DATE_10_MIN=`date --date='-10min' '+%H:%M:%S'`
DATE_10_MIN_HOURS=`date --date='-10min' '+%H'`
DATE_10_MIN_MIN=`date --date='-10min' '+%M'`https://stackoverflow.com/questions/56002178
复制相似问题