edaniels
edaniels

Reputation: 778

Finding intermediate exit status of command in series of ||

Given the following compound command:

try_this || or_this || fine_this

try_this will return 2, or_this will return 3, and fine_this will return 0.

How would one figure out the exit status of or_this after this execution? I'm under the impression it's not possible unless there is something similar to PIPESTATUS or pipefail for pipes.

Upvotes: 1

Views: 34

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295659

There's nothing built-in, but nothing stops you from storing the exit status somewhere on the side yourself. For instance:

storing_exit_status() {
  local dest=$1
  shift

  "$@"; local retval=$?

  printf -v "$dest" %d "$retval"
  return "$retval"
}

storing_exit_status try_this_retval try_this \
  || storing_exit_status or_this_retval or_this \
  || fine_this

echo "$try_this_retval"

Upvotes: 2

Related Questions