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?
# 1  
Old 12-07-2016
Hammer & Screwdriver How to exit the script incase of single failure?

i have a script that has 6 functions and 300 lines of code.

I am using bash shell.
Code:
 uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

The requirement i have is that the script should stop execution and exit as soon as even a single line of code fails. It should also display which particular line of code failed and in which function.

In the shebang shell there is this option while falls short of my requirement.

Quote:
Turn on -e mode
In this mode any command your script runs which returns a non-zero exitcode - an error in the world of shell - will cause your script to itself terminate immediately with an error.
You can do that in your shebang line:
#!/bin/sh -e
# 2  
Old 12-07-2016
Quote:
Originally Posted by mohtashims
In the shebang shell there is this option while falls short of my requirement.
And in which way does it do that? Sounds pretty much like your requirement in my ears.

bakunin
# 3  
Old 12-08-2016
Quote:
Originally Posted by bakunin
And in which way does it do that? Sounds pretty much like your requirement in my ears.

bakunin
How do i display the line of the script that failed??
# 4  
Old 12-08-2016
Quote:
Originally Posted by mohtashims
How do i display the line of the script that failed??
Use the PS4 prompt to display the internal variables BASH_LINENO and probably BASH_SOURCE. See man bash for details.

I hope this helps.

bakunin
# 5  
Old 12-08-2016
Quote:
Originally Posted by bakunin
Use the PS4 prompt to display the internal variables BASH_LINENO and probably BASH_SOURCE. See man bash for details.

I hope this helps.

bakunin
One of the functions in my script looks like the below.

Code:
#!/bin/bash -e
export PS4='$0.$BASH_LINENO+ '
echo "demo script"
ls -l /etc/ | wc -l
du -sh ~
grep er tpm.txt
echo i m good

What do i do to display the line number that failed ?

i tried adding export PS4='$0.$LINENO+ ' but it does not help nor i m any sure what needs to be done.

Last edited by mohtashims; 12-08-2016 at 09:46 AM..
# 6  
Old 12-08-2016
Quote:
Originally Posted by mohtashims
i tried adding export PS4='$0.$LINENO+ ' but it does not help nor i m any sure what needs to be done.
Call bash with the "-x" flag. bash (as well as ksh) will display every (debug-)line preceeded by the PS4 prompt in this case.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 12-08-2016
Tools

Quote:
Originally Posted by bakunin
Call bash with the "-x" flag. bash (as well as ksh) will display every (debug-)line preceeded by the PS4 prompt in this case.

I hope this helps.

bakunin
No ... i just wish to display the failing line ... along with the line number thats it... not the entire debug.

The sole and whole purpose is to Alert the caller which could be on a remote system calling this script using ansible or ssh know what line failed .

Last edited by mohtashims; 12-08-2016 at 11:27 AM..
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