Wednesday, 15 August 2012

bash's arcana with loop -



bash's arcana with loop -

have function loop:

runcfg () { o=0 while (( o<3 )); echo $o (( o++ )) done }

it calls script manager.sh by:

... -c|--runconfig) source $app_basedir/app-install/bin/config_functions; runcfg; shift;; ...

let's run it:

$ bash -x manager.sh -run

and @ output:

... + runcfg + o=0 + (( o<3 )) + echo 0 0 + (( o++ ))

that's lines... nil else happens.

but! if create little chage , add together && after arithmetic call:

runcfg () { o=0 while (( o<3 )); echo $o (( o++ )) && echo ++ok || echo ++er done }

it works!

... + runcfg + o=0 + (( o<3 )) + echo 0 0 + (( o++ )) + echo ++er ++er + (( o<3 )) + echo 1 1 + (( o++ )) + echo ++ok ++ok + (( o<3 )) + echo 2 2 + (( o++ )) + echo ++ok ++ok + (( o<3 )) + shift ...

what this?

your script configured exit on error (note prints ++er first time), , ((o++)) == ((0)), returns 1 (an error). options:

use let o++ instead. ignore error (((o++))||:) use loop (for ((o = 0; o < 3; o++))) turn off exit-on-error (set +e).

bash loops

No comments:

Post a Comment