Wednesday, 15 June 2011

conditional statements - Can I make bash report errors only errors at the end of a script? -



conditional statements - Can I make bash report errors only errors at the end of a script? -

i have bash script sequentially executes many tasks. however, because not want see simple status messages (such long output of yum -y update), ignored messages using:

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

this job fine, if went wrong, cp failed re-create file? if happens, grab error , exit immediately, before process continues.

how can exit process , show related error? normally, if/else clause have used, this:

#!/bin/bash yum -y update if [ $? -ne 0 ]; echo "error " exit 1 fi

but problem approach show process; therefore, have utilize > /dev/null on each line; more importantly, if had more 100 things do, have utilize many if/else statements.

is there convenient solution this?

rather running commands in (...) utilize set -e or bash -ec 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 alternative create sure exit sub shell error occurs.

bash conditional-statements

No comments:

Post a Comment