使用“set -o nounset”测试是否在 Bash 中设置了变量
以下代码退出并出现未绑定变量错误。如何在仍然使用 set -o
nounset 选项的情况下解决此问题?
#!/bin/bash
set -o nounset
if [ ! -z ${WHATEVER} ];
then echo "yo"
fi
echo "whatever"
The following code exits with a unbound variable error. How can I fix this, while still using the set -o
nounset option?
#!/bin/bash
set -o nounset
if [ ! -z ${WHATEVER} ];
then echo "yo"
fi
echo "whatever"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在这种情况下,如果未设置
WHATEVER
,VALUE
最终将成为空字符串。我们使用的是{parameter:-word}
扩展,您可以在“参数扩展”下的man bash
中查找该扩展。In this case,
VALUE
ends up being an empty string ifWHATEVER
is not set. We're using the{parameter:-word}
expansion, which you can look up inman bash
under "Parameter Expansion".如果你想得到你期望的结果,你需要引用变量:
测试:
You need to quote the variables if you want to get the result you expect:
Test:
假设:
如果您希望非交互式脚本在变量为空或未设置时打印错误并退出:
如果您不希望脚本退出:
您甚至可以使用
[
和 < code>] 而不是上面的[[
和]]
,但后者在 Bash 中更可取。注意上面冒号的作用。来自文档:
显然不需要
-n
或-z
。总而言之,我通常只使用
[[ "${VAR:?}" ]]
。 根据示例,如果变量为 null,则会打印错误并退出或未设置。Assumptions:
If you want a non-interactive script to print an error and exit if a variable is null or not set:
If you don't want the script to exit:
You can even use
[
and]
instead of[[
and]]
above, but the latter is preferable in Bash.Note what the colon does above. From the documentation:
There is apparently no need for
-n
or-z
.In summary, I may typically just use
[[ "${VAR:?}" ]]
. Per the examples, this prints an error and exits if a variable is null or not set.使用 oneliner:
-z
检查空变量或未设置的变量Use a oneliner:
-z
checks both for empty or unset variable您可以使用
但
可能更具可读性。
You can use
but
might be more readable.
虽然这完全不是所要求的用例,但我发现如果您想使用
nounset
(或-u
)默认行为是您想要的行为:以描述性消息退出非零值。如果您只想在退出时回显其他内容或进行一些清理,则可以使用陷阱。
否则
:-
运算符可能就是您想要的。While this isn't exactly the use case asked for, I've found that if you want to use
nounset
(or-u
) the default behavior is the one you want: to exit nonzero with a descriptive message.If all you want is to echo something else when exiting, or do some cleanup, you can use a trap.
The
:-
operator is probably what you want otherwise.对我来说,大多数答案充其量都是令人困惑的,不包括测试矩阵。它们通常也没有解决变量包含默认值的情况。
l0b0 的解决方案是唯一可读、可测试的(并且就实际问题而言是正确的),但尚不清楚如果反转/重新排序测试以简化逻辑会产生正确的结果。我缩小了他的解决方案
来自 Aleš 的(已经缩小的)对比解决方案暴露了已声明但未定义的变量的差异。其中之一可能适合您的场景。
Check1
和Check2
的行为相同Check3
显然是错误的Check4
:正确,取决于您认为声明/定义的内容多变的。To me, most of the answers are at best confusing, not including a test matrix. They also often do not address the scenario where variable contains the defaulting value.
The solution from l0b0 is the only readable, testable (and correct in respect to the actual question IMO), but it is unclear if inverting/reordering the tests to simplify the logic produces correct result. I minified hirs solution
The (already minified) contrast solution from Aleš, exposes the difference of a variable being declared but undefined. The one or the other might fit your scenario.
Check1
andCheck2
behave identicallyCheck3
is plainly wrongCheck4
: correct, depending on what you consider a declared/defined variable.这是我的贡献,当我开始使用数组/关联数组时,我需要一种简单的方法来获得答案。我的问题是:对于 0 个元素数组,这将返回错误:
单元测试:
输出:
实际示例:
This is my contribution bc once I began working with arrays/associative arrays, I needed an easy way to get the answer. My issue was: with 0 element arrays, this would return an error:
Unit Test:
Output:
Practical examples: