echo "Checking for ALERT messages in Logs:"
ls -d *COR* | while read file
do
grep ALERT $file/log/*COR*_$date1.log
if [ "$?" -ne "0" ]
then
echo $file - "No Alerts Found"
fi
done
output:
Code:
COR09 - No Alerts Found
[20110405092617]:COR10:8374:[ ALERT ] :CSTWCO00012:Exception processing trigger event
[20110405092617]:COR10:8374:[ ALERT ] :CSTWCO00013:Exception:: : no such function or object in (interactive) ( 0 )
RECOR_01 - No Alerts Found
grep: can't open RECOR02/log/*COR_20110405.log
RECOR_02 - No Alerts Found
In the output I would like it to have a header for when it finds alerts in the logs so in this case COR10:
Code:
[20110405092617]:COR10:8374:[ ALERT ] :CSTWCO00012:Exception processing trigger event
[20110405092617]:COR10:8374:[ ALERT ] :CSTWCO00013:Exception:: : no such function or object in (interactive) ( 0 )
Also for when it doesn't find a log to spit out "no longs found" instead of the grep: can't open.
Thanks in advance!
Last edited by Franklin52; 04-06-2011 at 04:16 AM..
Reason: Adding code tags
echo "Checking for ALERT messages in Logs:"
ls -d *COR* | while read file
do
grep ALERT $file/log/*COR*_$date1.log 2> /dev/null
case $? in
2) echo "No logs found";;
1) echo "$file - No ALERT found";;
esac
done
---------- Post updated 04-07-11 at 09:14 AM ---------- Previous update was 04-06-11 at 10:31 AM ----------
Ok the code worked perfect. Then I changed it to the following with the egrep and for some reason the case statement no longer works:
Code:
echo "Checking for ALERT messages in Logs:"
ls -d *COR* | while read file
do
egrep "ALERT|failed" $file/log/*COR*_$date1.log 2> /dev/null
case $? in
2) echo "$file - No logs found";;
1) echo "$file - No ALERT found";;
esac
done
It only spits out lines that find the ALERT or Failed but doesn't split out the no logs or no alerts found.
Can you please walk me through that code?
I honestly do'nt understand the $?
or how the 0 and 1 come into play and i want to understand my entire script.
In that case statement, I'd check for * instead of 1 in case it returned an error code greater than 1 for some reason.
Quote:
Originally Posted by cinderella
I honestly do'nt understand the $?
$? is a special variable meaning "the return status of the last command". Any command you run returns a status number, zero for success and anything else for error. Try this:
Code:
true
echo $?
false
echo $?
true and false are special commands that always return either success or failure.
The return status is implicitly checked when you do things like
Code:
if true
then
echo "true succeeded"
fi
if ! false
then
echo "false failed"
fi
true && echo "true succeeded"
false || echo "false failed"
Last edited by Corona688; 04-07-2011 at 02:31 PM..