help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there a way to capture all the exit status in an assignment?


From: Koichi Murase
Subject: Re: Is there a way to capture all the exit status in an assignment?
Date: Mon, 27 Apr 2020 01:05:37 +0900

2020-04-26 23:45 Peng Yu <address@hidden>:
> I'd like to capture all the exit status in an assignment like the
> following. It seems that something similar PIPESTATUS should be
> defined for this to work. Is it possible? Thanks.

I guess you are asking the way to collect the exit statuses without
rewriting the part `x=$(false)a$(true)b'.  I don't think there is a
way to achieve this without rewriting. And, of course, if it is
allowed to rewrite that part, you can easily collect the exit statuses.

x1=$(false); STATUS[0]=$?
x2=$(true); STATUS[1]=$?
x=${x1}a${x2}b
declare -p STATUS

Or

{ x=$(false;echo $? >&9)a$(true;echo $? >&9); } 9>tmpfile
mapfile -t STATUS < tmpfile
declare -p STATUS

Or

mkfifo tmp
exec 9<> tmp
rm tmp
x=$(false;echo $? >&9)a$(true;echo $? >&9)b
read 'STATUS[1]' <&9
read 'STATUS[2]' <&9
declare -p STATUS

Or

# Note: this is a simple implementation so
#   you cannot use ( or ) inside of $()
function csubstatus {
  local __head= __tail=$1
  local __reg='(\$\([^()]*\))(.*)'
  local __i=0 __result
  while [[ $__tail =~ $__reg ]]; do
    eval "__result[__i]=${BASH_REMATCH[1]}"
    CSUBSTATUS[__i]=$?
    __head=$__head${__tail::${#__tail}-${#BASH_REMATCH}}\${__result[$__i]}
    __tail=${BASH_REMATCH[2]}
    ((__i++))
  done
  eval "$__head$__tail"
}
csubstatus 'x=$(false)a$(true)b'
declare -p CSUBSTATUS

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]