# How to capture script return code?

**URL:** <https://community.unix.com/t/how-to-capture-script-return-code/314416>\
**Category:** Shell Programming and Scripting\
**Created:** [July 31, 2012, 7:31am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416 "2012-07-31T07:31:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nimo](https://community.unix.com/letter_avatar/nimo/32/6_449c533f586939b6e5f246632a46421f.png) [@nimo](https://community.unix.com/u/nimo)\
**Post date:** [July 31, 2012, 7:31am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/1 "2012-07-31T07:31:10Z")

</div>

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)

```nohighlight
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)

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

---

<div class="post-metadata">

**Author:** ![codecaine](https://community.unix.com/letter_avatar/codecaine/32/6_449c533f586939b6e5f246632a46421f.png) [@codecaine](https://community.unix.com/u/codecaine)\
**Post date:** [July 31, 2012, 7:59am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/2 "2012-07-31T07:59:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peasant](https://community.unix.com/user_avatar/community.unix.com/peasant/32/2251_2.png) [@Peasant](https://community.unix.com/u/Peasant)\
**Post date:** [July 31, 2012, 8:12am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/3 "2012-07-31T08:12:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nimo](https://community.unix.com/letter_avatar/nimo/32/6_449c533f586939b6e5f246632a46421f.png) [@nimo](https://community.unix.com/u/nimo)\
**Post date:** [July 31, 2012, 10:43am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/4 "2012-07-31T10:43:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![codecaine](https://community.unix.com/letter_avatar/codecaine/32/6_449c533f586939b6e5f246632a46421f.png) [@codecaine](https://community.unix.com/u/codecaine)\
**Post date:** [July 31, 2012, 7:27pm UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/5 "2012-07-31T19:27:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agama](https://community.unix.com/user_avatar/community.unix.com/agama/32/21613_2.png) [@agama](https://community.unix.com/u/agama)\
**Post date:** [July 31, 2012, 7:48pm UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/6 "2012-07-31T19:48:15Z")

</div>

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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![nimo](https://community.unix.com/letter_avatar/nimo/32/6_449c533f586939b6e5f246632a46421f.png) [@nimo](https://community.unix.com/u/nimo)\
**Post date:** [August 1, 2012, 8:40am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/7 "2012-08-01T08:40:56Z")

</div>

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

```nohighlight
brbackup -c -p profile

```

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

```nohighlight
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

```

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

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 1, 2012, 10:49am UTC](https://community.unix.com/t/how-to-capture-script-return-code/314416/8 "2012-08-01T10:49:17Z")

</div>

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

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

```
