Writing a UNIX script from LOG to provide return code.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing a UNIX script from LOG to provide return code.
# 1  
Old 04-11-2011
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
# 2  
Old 04-11-2011
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.

Code:
#!/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.
# 3  
Old 04-11-2011
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?
# 4  
Old 04-12-2011
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.

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

# 5  
Old 04-12-2011
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
Code:
#!/bin/sh
test -f "$1"  ||  exit 1
grep 'execution completed' "$1" | tail -n1 | grep -q successfully

2. 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)

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


2. 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... Smilie 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;

Code:
#!/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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script

I am using mutt on ksh Unix to send emails to addresses plucked from the database. If the "To:" email address is not longer valid and so the email is not sent to the "To:" recipient, but is sent to the valid cc address, I need to be able to get an error code returned to the shell script so that... (3 Replies)
Discussion started by: jzuber
3 Replies

2. Shell Programming and Scripting

Unix return code example

Hi, Does anyone here can guide me to understand how is return code works in a parent-child relation with a simple example? I have a request to build the script with return code in a child script, but i want to understand how does child script can return a code to the parent, stated if its... (4 Replies)
Discussion started by: khchong
4 Replies

3. HP-UX

return code from oracle to unix script

Hi I'm writing a shell script that connects to oracle database and fires query to check the availability of data in a table. In case of no data found then what will be the return code and how to handle in that in variable. Kindly provide with an example for better understanding... Thanks... (1 Reply)
Discussion started by: ksailesh
1 Replies

4. Shell Programming and Scripting

Pointers on writing a unix script

Hi All, I am writing a unix shell script. I have a file called Results.txt which stores 4 fields separated by pipe. i.e. a pipe delimited file. I want to loop through each record of this file and store each of these 4 fields into variables. I have read can be done through awk but I have never... (7 Replies)
Discussion started by: shwetainnani
7 Replies

5. Shell Programming and Scripting

need help writing this unix script

Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the file exists, prompt the user for confirmation before renaming the file. The screen should prompt the user for a. "Name of file you want to rename." Use the "\c"... (7 Replies)
Discussion started by: wiggles
7 Replies

6. Shell Programming and Scripting

help writing this unix script

I am working on writing scripts. Here is a script I need help with. I have also wrote what I think it is. I would really appreciate any help that I can get. Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the... (2 Replies)
Discussion started by: wiggles
2 Replies

7. UNIX for Dummies Questions & Answers

Perl Unix Script Writing

Hi Folks, I posted a few days ago, thanks for the responses. My original question was for renaming files of sort 3p2325294.dgn in a directory containing multiple files. I need to drop the first 2 characters and the last in a unix script using Perl. How does it differ from using the Unix... (1 Reply)
Discussion started by: Dinkster
1 Replies

8. Shell Programming and Scripting

return code from script

hey all, I have a simple script #!/bin/bash ./cron.sh test_a.txt & ./cron.sh test_b.txt & wait I want to invoke another program based on the return status of the above calls. How am I were to get that? Thanks! (2 Replies)
Discussion started by: mpang_
2 Replies

9. Shell Programming and Scripting

return code of a unix command

How to find out whether the command I executed is successful or unsuccessful(at commandlinet) Eg: say i execute the following command at command line rm * How do i find out whether my previous command is a success or failure. Thankyou. Best Regards, Ram. (1 Reply)
Discussion started by: ramky79
1 Replies

10. Shell Programming and Scripting

Need help in writing a unix script

OS: Solaris Shell : KSH Please help me in writing a script that captures a error message from a log file ( which updates continiously ) and send an email alert as soon as the systems throws a error message into that log. i.e With out monitoring the log Thanks in advance.. (1 Reply)
Discussion started by: pray44u
1 Replies
Login or Register to Ask a Question