Shell 脚本:未定义 -a、-o 切换行为
Shell scripts: undefined -a, -o switch behavior
根据 shellcheck wiki:
-a
and -o
to mean AND
and OR
in a [ .. ] test expression is not well defined. They are obsolescent extensions in POSIX and their behavior is almost always undefined.
知道会发生什么错误吗?
有关 -a
和 -o
可以提出的问题的讨论,请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16。通常,表达式的解析方式取决于任何参数扩展到的内容。考虑以下测试:
test "" -a ""
根据 </code> 的值,这可能是 </p>
<pre><code># Unspecified behavior
# With three arguments, the first "!", the second argument
# must be a unary primary like -n or -f.
test "!" -a ""
或
test "foo" -a "" # Test that "foo" and "" are non-zero length strings
如果 </code> 是 <code>!
,test
会将其视为否定运算符,而不是应检查其长度的字符串。
此外,test
仅适用于 4 个或更少的参数; -a
和 -o
通常会导致 5 个或更多参数被传递给 test
。
根据 shellcheck wiki:
-a
and-o
to meanAND
andOR
in a [ .. ] test expression is not well defined. They are obsolescent extensions in POSIX and their behavior is almost always undefined.
知道会发生什么错误吗?
有关 -a
和 -o
可以提出的问题的讨论,请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16。通常,表达式的解析方式取决于任何参数扩展到的内容。考虑以下测试:
test "" -a ""
根据 </code> 的值,这可能是 </p>
<pre><code># Unspecified behavior
# With three arguments, the first "!", the second argument
# must be a unary primary like -n or -f.
test "!" -a ""
或
test "foo" -a "" # Test that "foo" and "" are non-zero length strings
如果 </code> 是 <code>!
,test
会将其视为否定运算符,而不是应检查其长度的字符串。
此外,test
仅适用于 4 个或更少的参数; -a
和 -o
通常会导致 5 个或更多参数被传递给 test
。