문제

After an expression:

command1 | command2

I can retrieve the first commands exit status through ${PIPESTATUS[0]}

Is there an equivalent for expressions in the form:

command3 && command4 || command5

I would like to retrieve the exitcode of command3:

command3 && command4 || command5
retval=${SHORTCIRCUIT[0]}     # how to get this value?

Preferably without creating temporary files.

도움이 되었습니까?

해결책

Elaborating devnull's approach leads to

{ c3=0; command3 || c3=$? && false; } && command4 || command5
echo $c3

. Because every command can be replaced by

{ c=0; command || c=$? && false; }

(provided that c is initially unset if executed conditionally), this is easily extensible.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top