1

I have a bash script which sequentially executes many tasks. However, because I do not want to see simple status messages (such as the long output of yum -y update), I ignored all those messages using:

#!/bin/bash
(
 yum -y update
 cd /foo/bar
 cp ~/bar /usr/bin/foo
 ...
 ...
) > /dev/null

This does the job just fine, but what if something went wrong, like cp failed to copy some file? If this happens, I would like to catch the error and exit immediately, before the process continues.

How can I exit the process and show the related error? Normally, an if/else clause would have to be used, like this:

#!/bin/bash
yum -y update

if [ $? -ne 0 ]; then
    echo "error "
    exit 1
fi

But the problem with this approach is that it would show the process; therefore, I would have to use > /dev/null on each line; more importantly, if I had more than 100 things to do, then the I would have to use many if/else statements.

Is there a convenient solution for this?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
robue-a7119895
  • 816
  • 2
  • 11
  • 31
  • possible duplicate of [Automatic exit from bash shell script on error](http://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error) – arco444 Nov 14 '14 at 16:22

1 Answers1

1

Rather than running your commands in (...) use set -e OR bash -ec to execute them:

bash -ec 'set -e
yum -y update
cd /foo/bar
...
...
cp ~/bar /usr/bin/foo' > /dev/null 2> errlog

OR using set -e:

(
set -e
yum -y update
cd /foo/bar
...
...
cp ~/bar /usr/bin/foo
) > /dev/null 2> errlog

-e option will make sure to exit the sub shell as soon as an error occurs.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • But they way you are doing it would not be convenient to write 100 task in one line, and also how do i get the error when bash exist after it ? – robue-a7119895 Nov 14 '14 at 16:26
  • No need to write them on line, let me edit to clarify. – anubhava Nov 14 '14 at 16:30
  • Thanks for the clarification, but can I still catch the error before/after the `/dev/null` and show the error before it exists? – robue-a7119895 Nov 14 '14 at 16:33
  • `> /dev/null` is only redirecting stdout so stderr is still available for you to grab. – anubhava Nov 14 '14 at 16:33
  • Don't run with `bash -ec`. Instead, `set -e` inside your script. – gniourf_gniourf Nov 14 '14 at 16:34
  • But the question is, how would the error be caught and displayed at the end? I haven't dealt with `-ec` or `-e` flags before. – robue-a7119895 Nov 14 '14 at 16:35
  • 1
    @Contax: the script will just stop, and you'll have the error messages the commands send to stderr on your terminal. Just try it with a dummy example. – gniourf_gniourf Nov 14 '14 at 16:36
  • Yes `set -e` will also exit the script immediately. You can redirect the stderr using `2>errfile` – anubhava Nov 14 '14 at 16:37
  • @anubhava thanks. In the last line would changing this `2> errlog` to `echo 2>` work. I am clueless about this, I'have been using bash since a couple of hours. – robue-a7119895 Nov 14 '14 at 16:41
  • `2> errlog` will redirect stderr to a file called `errlog` that you can examine/process as per your need like `cat errlog` – anubhava Nov 14 '14 at 16:42
  • Ok, I didn't want to store the error, I just wanted it to get echo'ed as the script exists. But, I guess I could use cat for that. – robue-a7119895 Nov 14 '14 at 16:44
  • If you remove `2> errlog` from above script it will automatically print stderr on your terminal, nothing special is needed for that. – anubhava Nov 14 '14 at 16:47