do test
operators -a
, -o
short circuit?
i tried if [ 0 -eq 1 -a "" -eq 0 ]; ...
complained syntax of second conditional. can't tell if that's because
-a
not short circuit- or
test
wants formatted before begins , still short circuits.
the result leading me create nested if
when wanted situation first conditional guard against executing second if particular var had not yet been set...
edit: why using obsolescent operators, code has work everywhere in environment , found machine
while [ -l "$file" ] && [ "$n" -lt 10 ] && [ "$m" -eq 0 ]; do
is infinite loop , changing obsolete -a
yields behavior:
while [ -l "$file" -a "$n" -lt 10 -a "$m" -eq 0 ]; do
what should do? first expression works on many machines not machine appears require second expression instead...
per the posix specification test:
>4 arguments: results unspecified.
thus, barring xsi extensions, posix says nothing how behaves.
moreover, on system with xsi extensions:
expression1 -a expression2
: true if both expression1 , expression2 true; otherwise, false. -a binary primary left associative. has higher precedence -o. [option end]
expression1 -o expression2
: true if either expression1 or expression2 true; otherwise, false. -o binary primary left associative. [option end]
there's no specification respect short-circuiting.
if want short-circuiting behavior -- or posix-defined behavior @ -- use &&
, ||
connect multiple, separate test invocations.
quoting again, later in document:
application usage
the xsi extensions specifying -a , -o binary primaries , '(' , ')' operators have been marked obsolescent. (many expressions using them ambiguously defined grammar depending on specific expressions being evaluated.) scripts using these expressions should converted forms given below. though many implementations continue support these obsolescent forms, scripts should extremely careful when dealing user-supplied input confused these , other primaries , operators. unless application developer knows cases produce input script, invocations like:
test "$1" -a "$2"
should written as:
test "$1" && test "$2"
Comments
Post a Comment