Need help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help
# 1  
Old 04-05-2011
Need help

Here is my code:

Code:
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
# 2  
Old 04-05-2011
You could capture the grep output using backticks or $() to a variable and check the return code for success or not.

Also, you may be able to use find and/or xargs to make this program a bit easier (and more robust).

Is that enough help without giving out an exact answer? (trying to be helpful while allowing some learning to take place)
# 3  
Old 04-06-2011
Redirect stderr of grep into the black hole /dev/null. Look at grep's return value closer:
Code:
miro@miro-ntb:~$ grep SomeStringThatDoesntExist ~/.bashrc 
miro@miro-ntb:~$ echo $?
1
miro@miro-ntb:~$ grep a ~/someFileThatDoesntExist 2> /dev/null 
miro@miro-ntb:~$ echo $?
2

Together:
Code:
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


Last edited by mirni; 04-06-2011 at 04:12 AM..
# 4  
Old 04-07-2011
that was perfect! Thank you SO MUCH!

---------- 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.
# 5  
Old 04-07-2011
Code:
 
case $? in 
      0) echo "$file - ALERT or FAIL found";;
      1) echo "$file - not found";;
esac

# 6  
Old 04-07-2011
perfect!

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.

Thank you in advance.
# 7  
Old 04-07-2011
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..
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question