Writing a UNIX script from LOG to provide return code.

Folks -

Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX scripting and I have started learning it but this task seems to be a little complex for me.

I have the following log entry

##SAMPLE OF SUCCESSFUL LOG ENTRY##
4/11/11 3:19:07 PM EDT Version x.x.x
4/11/11 3:19:07 PM EDT Project Source Name: XYZ
4/11/11 3:19:07 PM EDT Login: ABC
4/11/11 3:19:07 PM EDT Executing task(s)...
4/11/11 3:19:07 PM EDT Checking syntax...
4/11/11 3:19:08 PM EDT Syntax is correct.
4/11/11 3:19:08 PM EDT Syntax checking has been completed.
4/11/11 3:19:08 PM EDT Event 'TEST' has been triggered successfully.
4/11/11 3:19:08 PM EDT No results returned.
4/11/11 3:19:08 PM EDT Task(s) execution completed successfully.
4/11/11 3:19:08 PM EDT Execution Time: 00:00:00
4/11/11 3:19:08 PM EDT Successfully disconnected. (MSTR) XYZ:ABC
#####################################################

##SAMPLE OF UN-SUCCESSFUL LOG ENTRY##
4/5/11 4:17:17 PM EDT Version x.x.x
4/5/11 4:17:17 PM EDT Project Source Name: XYZ
4/5/11 4:17:17 PM EDT Login: ABC
4/5/11 4:17:17 PM EDT Executing task(s)...
4/5/11 4:17:17 PM EDT Checking syntax...
4/5/11 4:17:17 PM EDT Syntax is correct.
4/5/11 4:17:17 PM EDT Syntax checking has been completed.
4/5/11 4:17:17 PM EDT (You do not have DssPrivilegesFireEvent privilege that is required to perform the task.)
4/5/11 4:17:17 PM EDT No results returned.
4/5/11 4:17:17 PM EDT Task(s) execution completed with errors.
4/5/11 4:17:17 PM EDT Execution Time: 00:00:00
4/5/11 4:17:17 PM EDT Successfully disconnected. (MSTR) XYZ: ABC
###################################################################

I need to somehow develop a UNIX script which will read the red line bolded in red above into

  1. 'Return Code' of ='0' for successful execution
  2. 'Return Code' of ='1' for un-successful execution

This log file will capture log information for all UNIX scripts which has been triggerred for the application I am monitoring.

I am open to discussion and design which you think will be the best method of approach to handle this task.

Thanks a lot
-Sree

Is there only one execution completed per log file? Or are there multiple executions?
If there's only one, then it's pretty simple.

Also, I assume you mean to return a binary return code, not text to stdout.

#!/bin/sh
test -f "$1"  &&  grep -q 'execution completed successfully' "$1"

Put that in a file, maybe name it checklog, use chmod to make it executable and use it like this:

checklog logfile

If the argument file doesn't exist, test -f will return 1 without doing the grep.
If the file exists and the quoted text is not found, grep will quietly return 1.
If both tests succeed, 0 is returned.

Thanks KenJackson!!

This code looks very straight-forward but the only problem is this particular log file will have entries for past executions as well. I am going to try to keep separate log file for different 'Events' to avoid the log file from capturing mutiple 'Event' executions at the same time.

I am jut throwing this idea out there, can we have a secondary log file which is generated from the master log file but it only captures the current execution with an override function? if this is somewhat possible we can potentially have the script you built to go against the secondary log file all the time.

This does sound a little complex to me, let me know what you think?

There are limitless ways you could approach it.

Here's a slight modification that uses grep to find all the execution completed lines and pipe them to tail, which only passes on the last one. Finally, grep tests to see if it was successful. So this will give a result of the last entry in the file.

#!/bin/sh
test -f "$1"  ||  exit 1
grep 'execution completed' "$1" | tail -n1 | grep -q successfully

Thanks Ken!

I believe that would do the trick!! I am trying to test this out from my environment and I have done the following;

  1. I have created a checklog.scp file with the following code below
#!/bin/sh
test -f "$1"  ||  exit 1
grep 'execution completed' "$1" | tail -n1 | grep -q successfully
  1. I made the above file executable

Now I am not quite sure about 2 things;

  1. If I should try to embed this code on my existing Unix script file (TR01.scp) which I created to trigger the event (See the Code Below)
ksh /export/home/install/bin/mstrcmdmgr 
-n DEV -u ABC -p xxxx 
-f /export/home/mstr/Trigger/Event/EV01.scp 
-o /export/home/mstr/Trigger/Logs/File_Output.scp
 
######
-f = Location of the Event script
-o = Location of the log file which will get generated
######
  1. Since your code does not specifiy the location of the log file to perform the grep, I am not sure where to place this file.

Ideally, I would like to see the return code when I execute the TR01.scp

Thanks for all your help Ken!!

---------- Post updated at 03:05 PM ---------- Previous update was at 08:56 AM ----------

Ken -

I finally broke the deadlock... :slight_smile: of course your idea was the inpiration to begin with. I tweaked it a little after getting some additional ideas. The script is as below;

#!/sbin/sh
###############################
HOME=/export/home/xxxx/Trigger
###############################

echo " Executing Script.. Please Wait... "
"$HOME/TRG01_PWS.scp"
 
echo " Executed Trigger TRG01_PWS "
 
echo " Verifying Execution.. Please Wait..."
sleep 20
 
tail -5 $HOME/Logs/File_Output.log > $HOME/Logs/run_state.log
strRunState=`grep "execution completed successfully" 
$HOME/Logs/run_state.log | wc -l`
 
if [ $strRunState == 1 ]
then
echo "Return Code = 0 (Trigger Executed Successfully)"
else
echo "Return Code = 1 (Trigger Execution Failed)"
fi

Let me know what you think?

Thanks for all your help!