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?