How to capture script return code?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to capture script return code?
# 1  
Old 07-31-2012
How to capture script return code?

Hi
I am executing database backup via shell script (Korn). The backup log is long, but I would like to capture only the last line so I can send an email if it fails

Example of failed backup (only last 3 lines)

Code:
BR0056I End of database backup: bejbofoh.aff 2012-07-26 07.31.21
BR0280I BRBACKUP time stamp: 2010-12-26 07.31.21
BR0054I BRBACKUP terminated with errors


Example of successful backup (only last 3 lines)

Code:
BR0056I End of database backup: bejckxdr.anf 2012-07-30 22.32.56
BR0280I BRBACKUP time stamp: 2008-01-30 22.32.56
BR0052I BRBACKUP completed successfully

How do I do that?

Last edited by Scott; 07-31-2012 at 10:17 AM.. Reason: Code tags
# 2  
Old 07-31-2012
After the program terminates. You can do echo $? to display the error code
# 3  
Old 07-31-2012
In case custom script is coded to return 0 for success Smilie
Please show the script and database type in question (i can presume oracle..)

Other then that you can use tail & grep for certain log and check their return codes ($?) as codecaine suggested.

Regards
Peasant.
# 4  
Old 07-31-2012
Hi
Thanks a lot for your help; using "echo $?" did half the job, which is capturing the return code. Is there a way to capture the last line from the backup log witout going to the log file?
# 5  
Old 07-31-2012
capture the line in what way? You can do export variable with the exit status of the last line.
# 6  
Old 07-31-2012
You mention a log file, so why not just something like this:

Code:
tail -1 log-file-name | mail -s "Backup finished: rc=$?" someuser@some.domain

Guess I should mention that the command will send the last line of the log file as the only line in the body of the email.
# 7  
Old 08-01-2012
Thanks again. My question is: when I run backup via shell script where I use the following command
Code:
brbackup -c -p profile

I get an output file, where somewhere there is a log file name


Code:
BR0280I BRBACKUP time stamp: 2012-08-01 08.14.30
BR0057I Backup of database: XXX
BR0058I BRBACKUP action ID: bejcrwgf
BR0059I BRBACKUP function ID: anf

Code:
BR0056I End of database backup: bejcrwgf.anf 2012-08-01 08.24.42
BR0280I BRBACKUP time stamp: 2012-08-01 08.24.43
BR0052I BRBACKUP completed successfully

I need to either capture the log file name or, even better, capture only the last line without going to the log file. How do I create a variable and do that?


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-01-2012 at 10:45 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to capture 3 digit return code of ftp??

is it possible to capture the 3 digit return code of ftp commands in a local variable inside a shell script? Code: ftp remoteserver << EOFTP quote USER uid quote PASS pass prompt cd remote_directory mput file.txt bye EOFTP in the above script, if cd ... (4 Replies)
Discussion started by: Little
4 Replies

2. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

4. Shell Programming and Scripting

Comparing files and capture return code

Hi, I would like to compare 2 files, and have a return code write to a file. regardless of the files contents are the same the code should be writing to a file (if both files contents are same then return code 0). A simple example will be great :) Thanks (3 Replies)
Discussion started by: khchong
3 Replies

5. Shell Programming and Scripting

How to capture C program return values in Kshell

I have a K shell script (ksh) that needs to return an email address. A C program was written (prog1) to now access the email address off of an oracle table. The call to the program in the ksh is prog1 -p parm1 Based on Parm1 the program will read an oracle table and retrieve the email... (2 Replies)
Discussion started by: jclanc8
2 Replies

6. Shell Programming and Scripting

PID Capture and Return Codes

I have a process that copies files from a main storage server to main other servers. We are attempting to speed up the processing and have thought that the best method would be to use concurrent file copying. What was suggested is that we change from using a simple RCP and waiting for it to... (3 Replies)
Discussion started by: dorrellg
3 Replies

7. Shell Programming and Scripting

capture ftp return code..PLZ HELP

Hi all, i have written a code to ftp a file from one server to other.The ftp is happeneing successfully,but i am not able to capture the return code,to check if ftp has failed. plz help me to find out the return code....this is urgent below is the code i have written... (3 Replies)
Discussion started by: anju
3 Replies

8. UNIX for Dummies Questions & Answers

how to capture the return value in master script?

Hi, Here is my program that is returning a DATE: #! /usr/bin/ksh db=tcore_dev1 bteq <<end .logon $password_core .export report file=$BTEQ_OUTPUT_FILE_$$ select cast(add_months(cast(substr(cast(max(closedate) as varchar(10)),1,8)||'01' as Date),1)-1 as timestamp(0)) from $db.repo... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

9. Shell Programming and Scripting

Capture Oracle return code in shell script

I am using the following code in my shell script list=`sqlplus -s $user/$pwd@$dbms<<EOF WHENEVER SQLERROR EXIT SQL.SQLCODE set pagesize 0 feedback off verify off heading off echo off select * from control_tbl where src_nm=$3 and extrct_nm=$4; exit SQL.SQLCODE; EOF` ERROR=$?... (1 Reply)
Discussion started by: Vikas Sood
1 Replies

10. Shell Programming and Scripting

Capture carriage return.

I try to test the carriage return in a variable. $ LENGTH=`expr $VARIABLE : ".*"` will return the length of the variable. But this doesn't work if $VARIABLE has zero length. Any help will be well appreciated. Thanks in advance. Giovanni (4 Replies)
Discussion started by: gio123bg
4 Replies
Login or Register to Ask a Question

Featured Tech Videos