codec
codec

Reputation: 8836

Capturing db2 command's return code in shell script

x=`su - db2inst1 -c "db2 get dbm cfg" |grep KEYSTORE_LOCATION | awk '{print $5}'`
rc="$?"
echo "${rc}"

this echos a 0.

If I forcefully execute a wrong command

x=`su - db2inst1 -c "db get dbm cfg" |grep KEYSTORE_LOCATION | awk '{print $5}'`
rc="$?"
echo "${rc}"

this gives me an error:

-bash: db: command not found

but still gives a 0 return code. I think its for the assignment. But how do I evaluate for the command.

Upvotes: 0

Views: 507

Answers (1)

John Kugelman
John Kugelman

Reputation: 362157

set -o pipefail

By default the return value of a pipeline is the exit code from the last command—here, awk '{print $5}'. Turn on pipefail to have the pipeline fail if any command fails, not just the last.

Upvotes: 2

Related Questions