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:
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:
I looked here and on Google, but could find nothing. Any help would be most appreciated.
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.
It would be better to call the check_err function only when you fail, something like this: -
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..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)