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";
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
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