Question

given the code below, I get "/usr/sbin/logcheck: line 21: syntax error at line 40: `else' unexpected" as the output. I have tried using elif instead of else but that didnt work either. Any help would be much appreciated.

#!/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
Was it helpful?

Solution

grep "$linestart" $logFilePath/$logFileName if [ $? != 0 ] then

If this line is correctly copy/pasted then this is where the error is. This runs grep on files named if and [, amongst others.

There needs to be a statement separator (semicolon or newline) between statements, such as before if, and also before then.

Explicitly examining $? is a common antipattern; the succinct and idiomatic way to write this conditional is

if ! grep "$linestart" $logFilePath/$logFileName; then
    ...

As a matter of coding style, using cat on a single file is useless; anything like cat file | grep regex is better written as grep regex file.

Also, error messages should properly be redirected to standard error (file descriptor 2):

echo "fail" >&2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top