3
#Example Script
wget http://file1.com
cd /dir
wget http://file2.com
wget http://file3.com

I want to execute the bash script line by line and test the exit code ($?) of each execution and determine whether to proceed or not:

It basically means I need to add the following script below every line in the original script:

if test $? -eq 0 
then
    echo "No error"
else
   echo "ERROR"
   exit
fi

and the original script becomes:

#Example Script
wget http://file1.com
if test $? -eq 0 
then
    echo "No error"
else
   echo "ERROR"
   exit
fi
cd /dir
if test $? -eq 0 
then
    echo "No error"
else
   echo "ERROR"
   exit
fi
wget http://file2.com
if test $? -eq 0 
then
    echo "No error"
else
   echo "ERROR"
   exit
fi
wget http://file3.com
if test $? -eq 0 
then
    echo "No error"
else
   echo "ERROR"
   exit
fi

But the script becomes bloated.

Is there a better method?

3
  • tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html Commented Sep 7, 2016 at 8:45
  • This is not quite the same as the 'flagged duplicate' - But because of it I can't add my own (properly formatted) answer! Commented Jan 19, 2017 at 0:28
  • I am actually surprised no one came up with a 'trap DEBUG' solution. You can use $? to get the wget exit value, $BASH_COMMAND to check that it is a wget, and $BASH_LINENO to tell you which wget. Commented Jan 19, 2017 at 0:29

5 Answers 5

2

One can use set -e but it's not without it's own pitfalls. Alternative one can bail out on errors:

command || exit 1

And an your if-statement can be written less verbose:

if command; then

The above is the same as:

command
if test "$?" -eq 0; then
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted. Though maybe emphasize even more that explicitly examining $? is almost never necessary, and that control statements such as if and while already do this, and that's their very purpose.
2

set -e makes the script fail on non-zero exit status of any command. set +e removes the setting.

4 Comments

If I use this method, how can I know which line (in the original script) causes the error? I want to print that line.
@J.Joe , You can use set -x to achieve that. So set -ex should do.
@choroba Actually, I think a better method will be 2>&1 | tee -a log so you can check the log anytime you want.
@J.Joe also have a look at stackoverflow.com/questions/10743026/….
2

There are many ways to do that.

For example can use set in order to automatically stop on "bad" rc; simply by putting

set -e 

on top of your script. Alternatively, you could write a "check_rc" function; see here for some starting points.

Or, you start with this:

check_error () {
  if [ $RET == 0 ]; then
    echo "DONE"
    echo ""
  else
    echo "ERROR"
    exit 1
  fi
}

To be used with:

echo "some example command"
RET=$? ; check_error

As said; many ways to do this.

1 Comment

If I use this method, how can I know which line (in the original script) causes the error? I want to print that line.
1

Best bet is to use set -e to terminate the script as soon as any non-zero return code is observed. Alternatively you can write a function to deal with error traps and call it after every command, this will reduce the if...else part and you can print any message before exiting.

trap errorsRead ERR;
function errorsRead() {

   echo "Some none-zero return code observed..";
   exit 1;
}


    somecommand #command of your need
    errorsRead  # calling trap handling function 

Comments

0

You can do this contraption:

wget http://file1.com || exit 1

This will terminate the script with error code 1 if a command returns a non-zero (failed) result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.