根据下面的代码,我得到“/ usr / sbin / logcheck:第21行:第40行的语法错误:'else'意外”作为输出。我尝试过使用elif而不是其他但是也没用。任何帮助将不胜感激。
#!/bin/ksh
# Set the config variables
# *************************************************Configuration********************************************************
logFileName="production.log"
errorList="ActionController::"
EMAIL_SUBJECT="Application ERROR"
EMAIL_TO="viva@viva.com"
# **********************************************************************************************************************
logFilepath="data/vbmapp/vb-mapp-portal/log"
# Set the Log File path
if [ ! -s $logFilePath/$logFileName ]; then echo "ERROR- Log File Not Found , Please set the config properly"
exit
fi
# Get the first 30 characters of the first line linestart=$(awk 'NR>1{exit} ;1' $logFilePath/$logFileName | cut -c1-30)
lineend=""
# Never ending loop that will parse the Out.log file every 5 sec
while true ; do
# get the last line of file , till which we need to parse the log in this iteration
lineend=$(awk 'END{print}' $logFilePath/$logFileName | cut -c1-30)
# if log file not found , Do nothing and wait for the next iteration
if [ ! -s $logFilePath/$logFileName ]; then echo "Log File Not Found .. Waiting for the next iteration ..."
fi
# error checking , in case we dont find the linestart , parse the whole file
grep "$linestart" $logFilePath/$logFileName if [ $? != 0 ] then
echo "cat $logFilePath/$logFileName | egrep $errorList | /usr/sbin/sendmail -s $EMAIL_SUBJECT $EMAIL_TO"
cat $logFilePath/$logFileName | egrep "$errorList" | /usr/sbin/sendmail -s $EMAIL_SUBJECT $EMAIL_TO
else
#parse the log file from linestart to lineend for errors
echo 'awk "/$linestart/,/$lineend/" $logFilePath/$logFileName | egrep "$errorList" | /usr/sbin/sendmail -s $EMAIL_SUBJECT $EMAIL_TO'
awk "/$linestart/,/$lineend/" $logFilePath/$logFileName | egrep "$errorList" | /usr/sbin/sendmail -s $EMAIL_SUBJECT $EMAIL_TO
#set the last line as the first line for next iteration
linestart=$lineend fi
#set the last line as the first line for next iteration
linestart=$lineend
sleep 5
done
答案 0 :(得分:2)
grep "$linestart" $logFilePath/$logFileName if [ $? != 0 ] then
如果正确复制/粘贴此行,那么这就是错误所在。这会对名为grep
和if
的文件运行[
。
语句之间需要有一个语句分隔符(分号或换行符),例如if
之前,then
之前。
明确检查$?
是一种常见的反模式;写这个条件的简洁和惯用方法是
if ! grep "$linestart" $logFilePath/$logFileName; then
...
作为编码风格的问题,在单个文件上使用cat
是useless;像cat file | grep regex
这样的内容最好写成grep regex file
。
此外,错误消息应正确地重定向到标准错误(文件描述符2):
echo "fail" >&2