Abort the execution if one script have errors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Abort the execution if one script have errors
# 1  
Old 03-05-2015
Abort the execution if one script have errors

Gents,

I have a script which will run others scripts .

Example

Code:
#!/bin/bash

script1
script2
script3
script4

I would like to stop all process if any of the script got errors?. If for example script1 works fine will continue script2, then if the script2 got errors, The bash should stop and will no continue to script3.

Is there any solution for this.

Thanks for your help.
# 2  
Old 03-05-2015
How will you know if script2 errors?

If it issues a return code, this will be in the variable $? until you do something else. You can test for that:-
Code:
:
:
scripts2
if [ $? -ne 0 ]
then
   any abort action or message
   exit
fi
script3
:
:

You can make your script exit with a non-zero return code by putting in exit 5 or whatever integer value you want (within reason)

Another way to do this is to make them dependant on each other with the && operator. This will move to the next if the return code is zero but quit that statement (carries on the the overall script) if any one of them fails:-
Code:
script1 && script2 && script3 && script4

echo "This always runs"


Do either of these meet your needs?


Regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 03-05-2015
Did you consider bash's -e option?
Quote:
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with a
non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of
the test following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any
command in a pipeline but the last, or if the command's return value is being inverted with !. If a compound command other than a subshell returns a
non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell
exits. This option applies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause
subshells to exit before executing all the commands in the subshell.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If Telnet fail abort other command..??

Hi Team, I my script i telent my node ip and after loging i run some commands...But some time login fail so i want to stop script at that time.. CONFIG_CHNG.sh printf 'Please Input Node ID : ' read node node="$node" #exec > node_log_$node.log 2>&1 #set -x >... (2 Replies)
Discussion started by: Ganesh Mankar
2 Replies

2. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

3. Shell Programming and Scripting

How to Abort or Come out of ksh script?

Hi All, I have a requirement where if Source Count is not equal to Target count, then I need to exit/abort the script and come out. Then send an email to thummi@email.com saying "RECORD COUNT DOES NOT MATCH. Please need your help. Here is what I have written : If even the count is not... (1 Reply)
Discussion started by: thummi9090
1 Replies

4. Shell Programming and Scripting

Shell Script to Abort if file name has space.

Hi, In my shell script I use the following code to copy files from one directory to other. for file in `ls`; do #----------------- Copy Files From INDIR to OUTDIR -------------# echoen "Copying File ${INDIR}/$file to ${OUTDIR}/${file}" cp ${INDIR}/$file ... (4 Replies)
Discussion started by: pinnacle
4 Replies

5. Solaris

Abort the shell script if any hive sql query gets failed

Below is my shell script from which I am trying to invoke few Hive SQL queries and the below shell script works fine. Problem Statement:- If you see my first `hive -e` block in the below shell script which contains a very long Hive SQL query. Sometimes that Hive SQL query gets failed due to... (1 Reply)
Discussion started by: raihan26
1 Replies

6. Shell Programming and Scripting

Help with execution errors in script

solution found.... Please use tags for scripts, listings, and console output (2 Replies)
Discussion started by: audiolord
2 Replies

7. Shell Programming and Scripting

How to handle errors during script execution

Hi I have written a script which returns a number. This resulting number I assign to the variable for example: $ A=`get_dbnum 118 ttrn` $ echo ${A} 8208 $ The script becomes interective once the error occures and the number is not found. It prompts the user to enter the number. The... (1 Reply)
Discussion started by: aoussenko
1 Replies

8. Shell Programming and Scripting

How to make bash script abort?

I have a little bash script that includes make to compile with g++ and then a statement to actually run the compiled program. When it (the script) gets a syntax error, it does not abort and continues to run the previous version of the program. How can I make the script abort when g++ generates a... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Filesystems, Disks and Memory

Abort core dumped!!!!

HI All, I am working on Solaris 8, i have this application runing on one of the partitions,(the installation was done here ie /export/home) And the out put of this goes to another parition of other disk attached to the same machine. After a certain period of time is get this error stating... (2 Replies)
Discussion started by: zing
2 Replies
Login or Register to Ask a Question