Grep in Shell script

hi guys

very new to this game so excuse my ignorance. I need to create a script that simply greps for a text string and then outputs a message depending on whether the text string is there or not. The script I have setup is below, but whenever I run it I get the following error:

./LogCheck.sh[8]: TIMEOUT=:  not found
./LogCheck.sh[10]: ${TIMEOUT}: bad substitution

Sure this is a schoolboy error, but any advise greatly appreciated

Thanks,
A.

#!/bin/ksh
##################################################################################################
                                                                                                #
# This script checks the log file for timeouts                           #
##################################################################################################
 
TIMEOUT=`tail -5000 /dir1/logs/logfile.log | grep "has timed out and is not currently tradeable" | tail -1`;
 
echo ${TIMEOUT};
 
if [ ! -z ${TIMEOUT} ]; then
        echo "Timeout Alert";
else
        echo "No Timeouts";

Try this,

TIMEOUT=`tail -5000 /dir1/logs/logfile.log | grep "has timed out and is not currently tradeable" | tail -1`;
echo ${TIMEOUT};
[[ ! -z ${TIMEOUT} ]] && echo "Timeout Alert" || echo "No Timeouts"

Also you need to avoid those semi-colons, their unnecessary in shell scripting and double quoting the variable ${TIMEOUT} in the if condition is a good practice..and finally there is no closing if (fi)

TIMEOUT=`tail -5000 /dir1/logs/logfile.log | grep "has timed out and is not currently tradeable" | tail -1`
echo ${TIMEOUT}
 
if [ ! -z "${TIMEOUT}" ]
then
        echo "Timeout Alert"
else
        echo "No Timeouts"
fi

You can also do this:

if TIMEOUT=$(tail -5000 /dir1/logs/logfile.log | grep "has timed out and is not currently tradeable" | tail -1)
then
  echo "Timeout Alert"
else
  echo "No Timeouts"
fi

if you need the TIMEOUT variable afterwards, or

if tail -5000 /dir1/logs/logfile.log | grep -q "has timed out and is not currently tradeable" 
then
  echo "Timeout Alert"
else
  echo "No Timeouts"
fi

if you don't need the variable..

==
Edit: The second case will work, but the first will not because of the tail -1 command which will return 0 even if there is no match

Thanks for getting back, Scrutinizer - looks a little better - when I try your script with the TIMEOUT variable, I get his message:

./LogCheck.sh[8]: TIMEOUT=: not found
No Timeouts

Is the "not found" message just saying that is just can't find the "has timed out string" in the log file?

Thanks

---------- Post updated at 03:30 PM ---------- Previous update was at 03:09 PM ----------

jsut played again and it works Ok thanks. Appreciate your help

Actually I realized the first command will not work properly because of the tail command, I edited the post...