bash - How to use detect failure in command substitution -


as know set -e useful discover failure when command executed. found delicate use , don't know how use in following scenarios:

==============first example================================

set -e function main() {       local x1="$(exp::notdefined)"       echo "will reach here : ${lineno}"       x2="$(exp::notdefined)"       echo "will not reach here : ${lineno}" } main  

==============second example================================

set -e     function exp::tmp() {       echo "now '$-' "$-" : ${lineno}"       false       return 0 }     function main() {       x1="$(exp::tmp)"       echo "will reach here : ${lineno}. '\$x1' : ${x1}"       x2="$(set -e ; exp::tmp)"       echo "will not reach here : ${lineno}" }     main 

===============================

the first example shows that, if use command substitution on local variable, not fail if command substituted not found. don't know how detect these kinds of failures.

the second example shows that, if bash options (-e) not propagate unless call set -e inside command braces. there better way this?

you request immediate exit on pipeline failure -e, e.g.:

-e      exit if pipeline (which may consistof single simple         command), list, or compound command (see shell grammar above),          exits non-zero status. 

the reason bad command substitution not cause failure within function because local provides own return status.

local [option] [name[=value] ...]
... return status 0 unless local used outside function, invalid name supplied, or name readonly variable.

the assignment of failed command substitution does not cause local return non-zero. therefore, no immediate-exit triggered.

as far checking failure of command substitution following local, since output assigned variable, , return not non-zero in event of command substitution failure, have validate checking variable contents expected values success/failure.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -