How to exit the script incase of single failure?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to exit the script incase of single failure?
# 8  
Old 12-08-2016
Quote:
Originally Posted by mohtashims
No ... i just wish to display the failing line ... along with the line number thats it... not the entire debug.
Use grep to filter out the unneeded information....
# 9  
Old 12-08-2016
Quote:
Originally Posted by rovf
Use grep to filter out the unneeded information....
What are the strings i need to grep for apart from "error" ? Also, if i were to grep the output it has to be outside the script that i m running. Instead, i wanted to highlight the failing command as the output on the same script.

This is what i was looking for. Like after every lines of your script you do a echo $? and report the non zero i.e error causing line of code in the same running script. While this would be trivial to write the logic after every line i thought of asking here if someone knows a better solution.
# 10  
Old 12-08-2016
Since your progams will also write to stdout, I would filter out every line coming from the trace line, except those which show the error. Note that you can, by configuring PS4, design the trace line in a way that it is very unlikely that a "real" output has the same structure.
# 11  
Old 12-08-2016
Quote:
Originally Posted by mohtashims
i just wish to display the failing line
I want to eat pork and curse Allah while enjoying my 72 virgins - some things just aren't going to be together.

You might succeed with a trap function, but any solution i can think of is not going to be "plugged into" an existing script with less than heavy modifications.

What i do in these cases is: write to log files, so that - when a script fails, i can trace how much of its task was completed until it failed. It may still fail but this usually makes it way easier to clean up afterwards.

Second, i identify what i (respectively the script) need(s) to know beforehand and i test/execute all these tasks first. This way, when i finally do something that might fail, i have made as sure as is possible to make it succeed - or, if not all preliminary tasks are successful, don't even attempt to do it.

For example: i need to write a file. Before even writing it, i test if the directory i want to write to is there and can be written. Then i test if enough space is available to write it. Then i make sure there is no file with the name i want to write to and eventually save an existing one to a different name (or not and raise an error). Etc., etc., only then i finally write the file. All these preliminary tasks, if unusuccessful, will lead to a program stop and a respective error message ("cannot write to dir", "not enough space",...).

In general: plan your programs so that they do not fail instead of analysing errors. Doing it the way i described it is - for my purposes - usually sufficient and easy to analyse in case something indeed goes wrong (because regardless of how much precaution you take there is always the possibility of an error, that is just how life is).

It usually helps in planning to try to create a list of things that could go wrong - and then write code to react to exactly this.

Another example: task is to create a user account. Here is, off the top, a (surely incomplete) list of thing that could be/go wrong and possible reactions of the program. Your reactions might look different, depending on the details of your environment:

- necessary command not there (-> reinstall part of the OS)
- necessary information not provided (-> raise error, stop)
- user already exists (-> raise warning, stop)
- user id already taken (-> raise error, stop)
- script called with not enough rights (-> raise error, stop)
- home directory cannot be created (for various reasons) (-> raise error with exact reason, stop)
- user exists but has different attributes (-> raise warning, change attributes, continue)

Wheever i identify such a task i do not write a script, but i write a function with a well-documented interface which will implement exactly this one task. Further scripts will then only call this function with the appropriate set of parameters.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 12  
Old 12-08-2016
Fully supporting what bakunin (and others) said, would this help to fulfill your primary needs? It prints the line No. in which an error occurred, and with the -e option set it quits the script:
Code:
#! /bin/bash
[ -e VX ] && set -vx
# declare -A P


trap "echo error in line \$LINENO"  err

ls rgsfbtf
ls .
set -e

ls sefrg
ls .
.
.
.

./shscr werf weqrr qer
ls: cannot access 'rgsfbtf': No such file or directory
error in line 8
actTL  awk  Awk.sh  bl.sh  bl.sh~  file~  file1  file1~  file2	file2~	file3  file3~  file4  file4~ 
ls: cannot access 'sefrg': No such file or directory
error in line 12

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Re run the script from the point of failure

Hello I have a shell script with multiple sections in it. Like, verify pre-requisites, ch co version, stop services , install product , post migration steps, start services, send status email. If the script fails at certain step, (like after product installation) it can't be re-run since the... (2 Replies)
Discussion started by: mo12
2 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

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

5. Shell Programming and Scripting

bash script - sftpbatchfile - stop on failure

Hello all, I am currently writing a script to send files to a server over sftp. When the sftp put command succeeds it wil preform a local move from within the sftp shell to another folder (this is done so when the script is rerun no doubles will be sent). therefore i had following sollution ... (32 Replies)
Discussion started by: Kerberos
32 Replies

6. UNIX for Dummies Questions & Answers

boot up failure unix sco after power failure

hi power went out. next day unix sco wont boot up error code 303. any help appreciated as we are clueless. (11 Replies)
Discussion started by: fredthayer
11 Replies

7. Shell Programming and Scripting

Expect script file transfer failure

I use expect to spawn an sftp file transfer. On occasion the transfer fails or doesn't complete correctly, resulting in: "sftp> cd /returns sftp> rename /returns/TESTFILE.TXT /returns/archive/TESTFILE.TXT Couldn't rename file "/returns/TESTFILE.TXT" to "/returns/archive/TESTFILE.TXT": Failure... (0 Replies)
Discussion started by: tjb1959
0 Replies

8. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

9. Shell Programming and Scripting

Display LineNo Incase Total Number Of Delimiter Does matches in a given variable

I have many files .dat extension. requirement is to display line no if no of delimiter does not matches in a given variable lets say File: REF_BETOS.dat HCPCS_OR_CPT_CODE~BETOS_CODE~TERMINATION_DATE 0001F~Z2~ 0003T~I4~B20061231 0005F~Z2~~~ 0008T~P8~B20061231... (1 Reply)
Discussion started by: ainuddin
1 Replies

10. UNIX for Dummies Questions & Answers

trying to grep the first few lines of a continuos script, and exit the script anyidea

Hi. I am trying to extract the output of the first few lines of a continuos sh script. The when i run it, i wont to grep the the first 20 lines of it for an entry and basically do a crtl z out of it or to that effect, and output the results to a text file. I basically want to script... (5 Replies)
Discussion started by: k00061804
5 Replies
Login or Register to Ask a Question