Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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?

like image 548
sorin Avatar asked Jul 31 '26 04:07

sorin


1 Answers

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
like image 80
K.Hacene Avatar answered Aug 02 '26 18:08

K.Hacene