How to get the status of a subcommand in a bash script that is invoked with -e? -


i want write bash script -e option stops on error, want allow specific line return error code different 0 , use in logic:

#/bin/bash -e do_something_and_return_errcode if [ $? -eq 2 ];     specific_stuff fi 

the script end in do_something_and_return_errcode. avoid doing

do_something_and_return_errcode || true 

but make $? return 0 , cannot use $pipestatus because not pipeline.

first, using set -e highly error-prone, , not universally agreed on practice.

that said, consider:

err=0 do_something_and_return_errcode || err=$? 

another option temporarily disable flag:

set +e ## disable effect of bash -e / set -e do_something_and_return_errcode; err=$? set -e ## ...and reenable flag after 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -