sorin
sorin

Reputation: 170400

Optimal way to save command exit code in scripts using set -e (trap)?

Is there a better/nicer way to save the return code of a specific command inside bash scripts that use the set -e?

As you know when in trap mode the script will exit unless you use a pipe and and the piped command succeeds.

#!/bin/bash
set -e
# a lot of code...
MY_RESULT=0
command-that-can-fail || MY_RESULT=$?
export MY_RESULT
# optionally 
printf "MY_RESULT=${{MY_RESULT}}\n" >> env.property 

Background: This is related mostly to CI/build environment script where not using set -e is bad because it does hide error and the script continues even when you had some errors.

Now, the only problem with the code above is that's ugly/harder to read and that in many cases the pipe commands is even outside the screen visible are and people get confused about how the code works.

So, the question is how to rewrite this in order to improve readability. Can we bring some beauty to bash?

Upvotes: 3

Views: 495

Answers (1)

K.Hacene
K.Hacene

Reputation: 135

You can use the '!' special character to avoid exiting your script, and group the instructions to get back the return value :

#! /bin/sh
set -e
mycmd()
{
  if [ $1 -eq 1]; then
    return 1
  fi
  return 0
}

err=0
! {
  mycmd 0
  err=$?
}
echo "err $err"   # Err = 0

err=0
! {
  mycmd 1
  err=$?
}
echo "err $err"   # Err = 1

If you're using "exit" instead of "return", don't forget to use a sub-shell for the call of your command :

mycmd()
{
  if [ $1 -eq 1]; then
    exit 1
  fi
  exit 0
}

err=0
! {
  $(mycmd 1)
  err=$?
}
echo "err $err"   # Err = 1

Upvotes: 2

Related Questions