Want to trap script error and return line number of failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to trap script error and return line number of failure
# 1  
Old 11-12-2009
Want to trap script error and return line number of failure

Hey all UNIX nerds- I've built a shell script which runs pretty well- only I want it to have much better error trapping. (Like the kind I could apply to every shell script I write).
I'm not a UNIX genius, and could really use a bit of help.

The original script goes something like this:

Code:
1  #VARIABLES
2 
3  export EMAIL_ADDRESS=somebody@somewhere.com
4  export LOG_DIR_NAME=backup_logs
5  #--------------------------------------------------------------------
6  #FUNCTIONS
7 
8  check_err()
9  {
10 LINE_NUMBER=$2
11 MESSAGE="Script failed at line number $LINE_NUMBER on "`date +"%m_%d_%y_%R:%S"`". Please check the logs located at "`pwd`"/$LOG_DIR_NAME/ for more details."
12 SUBJECT="FAILED BACKUP ON $HOSTNAME at Line $LINE_NUMBER"
13 if [ $1 -ne 0 ]; then
14 echo "$MESSAGE"| mailx -s "$SUBJECT" "$EMAIL_ADDRESS"
15 exit 1
16 fi
17 }
18 #--------------------------------------------------------------------
19 #Begin Script
20 
21  ...command1
22      check_err $? $LINENO #If it fails here, it will tell you it failed at line 22
23  ...command2
24      check_err $? $LINENO #If it fails here, it will tell you it failed at line 24
25  ...command3
26  ...command4
27      check_err $? $LINENO #If it fails here, it will tell you it failed at line 27
28 
29 #End Script


Note that this script sets a bunch of required variables first, then creates a function named check_err which will e-mail me if there's ever a problem with the script.
Note that the check_err function accepts two input parameters: the error number and the line number where the failure occurred.
...So far, pretty darn good.
But I don't want to have to call the check_err function (with the parameters error number and line number) after every command in the script.
And you can also see the limitations of the above method: If the e-mail says it failed at line 27, did it actually fail at line 24, or line 25? ...Its just saying that it failed at line 27 because that's the line the check_err function got run from.

What I probably want to do is have it execute in some kind of loop, and then, if there is an error, pass the error number and the EXACT line number to the check_err function to tell me EXACTLY where it failed, and why- and then exit.
I also don't want it to run the script in a loop forever, so there would have to be some kind of break condition...

You'll note I attempted something like this- which mostly works, except the downside is that the line number where it failed is never going to be accurate:

Code:
1  #VARIABLES
2 
3  export EMAIL_ADDRESS=somebody@somewhere.com
4  export LOG_DIR_NAME=backup_logs
5  #--------------------------------------------------------------------
6  #FUNCTIONS
7 
8  check_err()
9  {
10 LINE_NUMBER=$2
11 MESSAGE="Script failed at line number $LINE_NUMBER on "`date +"%m_%d_%y_%R:%S"`". Please check the logs located at "`pwd`"/$LOG_DIR_NAME/ for more details."
12 SUBJECT="FAILED BACKUP ON $HOSTNAME at Line $LINE_NUMBER"
13 if [ $1 -ne 0 ]; then
14 echo "$MESSAGE"| mailx -s "$SUBJECT" "$EMAIL_ADDRESS"
15 exit 1
16 fi
17 }
18 #--------------------------------------------------------------------
19 #Begin Script
20 
21 TIMES_AROUND=0
22 
23 while check_err $? $LINENO #With this code, whenever there is a failure, it will always tell you that it failed at line 23. ...Not really what we want.
24 do
25   TIMES_AROUND=`expr $TIMES_AROUND + 1`
26     if [ $TIMES_AROUND -lt 2 ]; then
27         echo "Hello This is the normal script run."
28         echo "The current iteration of this run is: "$TIMES_AROUND
29         FAIL! #Test hypothetical failure condition. The script will fail here, and send me an e-mail, then exit- which is *exactly* what we want- only, the line number at which it failed won't be 29- it will be line 23, where the "while check_err $? $LINENO" syntax is.
30     else
31         break
32    fi
33 done
34 
35 #End Script

I looked here and on Google, but could find nothing. Any help would be most appreciated.


Thanks!
# 2  
Old 11-13-2009
Try set -x and just write everything to a log
Code:
#!/bin/ksh
set -x
exec 2>&1 > mylogfile
..........  code here

As to email, just be sure your script returns non-zero when it fails

Code:
myscript.sh parm1 || /usr/bin/uuencode mylogfile mylogfile |/usr/bin/mailx -s 'script failed' me@mycompany.com

# 3  
Old 11-13-2009
Want to trap script error and return line number of failure

Jim- I see what you're saying, but I really want to get the line number where it failed, and also (if possible) make it uncomplicated to call the script.

Doing it this way should make debugging and using the script a breeze.

...This is a script that will end up on several servers, so having it be correct, and having it easy to call is really what I'm looking for.
# 4  
Old 11-15-2009
It would be better to call the check_err function only when you fail, something like this: -

Code:
some command || check_err $? $LINENO "msg"

In fact I use this syntax without the line number or exit code, just put what I want in the error msg, that way the error handler is generic. Mine is usually called tidyupexit and will handle cleaning up my temp files etc before exiting as well as giving the error message. If each error message is unique then you do not need the line number, just grep for the message in the offending script and it takes you to the heart of the problem.

Last edited by steadyonabix; 11-15-2009 at 08:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Syntax error in one line in sudoer file cause total failure

I have notice that when I create a sudoer file in the sudoer.d directory, then if I have a syntax error, I cannot do sudo at all, in all accounts. Why can't they change the mechanism, so it will ignore syntax error line and will only display error message but won't cause total failure and... (7 Replies)
Discussion started by: programAngel
7 Replies

2. Shell Programming and Scripting

Pass back return code for file delete failure

Hello Experts, This script to delete a file is submitted from an SAP system which has 2 servers. When it happens to run on server 1, the delete is successful. When it runs on server 2, the delete always fails. All user accounts and permissions have been adjusted to match on both servers. Is it... (5 Replies)
Discussion started by: SAPDEVEL
5 Replies

3. Shell Programming and Scripting

Trap Oracle error in shell script

sqlplus -s usrname/password@dbSID <<-SQL >> logfile @create_table.sql commit; quit; SQL I am running this script to execute an sql file. I want to display the oracle error if anything found during execution of the sql file and exit from script. Please suggest How do it. (1 Reply)
Discussion started by: millan
1 Replies

4. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

5. Red Hat

KSH script help needed ( nice error trap routine ?)

I am running a script that runs a loop and executes a command on ${i} until the end of the for loop. From time to time the command generates an error ( which is good) for example ERROR0005: How can I trap the error and send an email echoing the ${i} variable in the loop and the error ? ... (2 Replies)
Discussion started by: pcpinkerton
2 Replies

6. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

awk script to return the middle line number

I need an awk script that returns the 1st field of the line in the middle of a file. For example, if a file I have has 6 lines, I want it to return the 1st field on line number 3. If a file has 7 lines, I want the script to return the 1st field on line number 4. File1: 3 214 4 219 5 226 2... (8 Replies)
Discussion started by: jontjioe
8 Replies

8. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

9. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question