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)

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)

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?

After the program terminates. You can do echo $? to display the error code

In case custom script is coded to return 0 for success :slight_smile:
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.

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?

capture the line in what way? You can do export variable with the exit status of the last line.

You mention a log file, so why not just something like this:

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.

Thanks again. My question is: when I run backup via shell script where I use the following command

brbackup -c -p profile

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

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
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?

Every program returns an error code when it quits. You may not actually need to grep the logfile for anything. Try this:

if brbackup ... # Some backup command guaranteed to fail
then
        echo "brbackup succeeded'
else
        echo "brbackup failed"
fi