测试以查看是否在 bash 中设置了 env 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7520356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing to see if an env variable is set in bash
提问by Neil Traft
In a bash script, I'm trying to test for the existence of a variable. But no matter what I do, my "if" test returns true. Here's the code:
在 bash 脚本中,我试图测试变量是否存在。但无论我做什么,我的“if”测试都会返回 true。这是代码:
ignored-deps-are-not-set () {
if [ -z "${ignored_deps+x}" ]
then
return 0
fi
return 1
}
ignored_deps=1
ignored-deps-are-not-set
echo "ignored-deps function returns: $?"
if [ ignored-deps-are-not-set ]
then
echo "variable is unset"
else
echo "variable exists"
fi
Here is the output as written:
这是所写的输出:
ignored-deps function returns: 1
variable is unset
And the output when I comment out the line where ignored_deps is set.
当我注释掉设置 ignore_deps 的行时的输出。
ignored-deps function returns: 0
variable is unset
No matter what, it says the variable is unset. What am I missing?
无论如何,它说变量未设置。我错过了什么?
回答by Jonathan Leffler
This line:
这一行:
if [ ignored-deps-are-not-set ]
tests whether the string 'ignored-deps-are-not-set' is empty or not. It returns true because the string is not empty. It does not execute a command (and hence the function).
测试字符串 'ignored-deps-are-not-set' 是否为空。它返回 true,因为字符串不为空。它不执行命令(因此不执行函数)。
If you want to test whether a variable is set, use one of the ${variable:xxxx}notations.
如果要测试是否设置了变量,请使用其中一种${variable:xxxx}表示法。
if [ ${ignored_deps+x} ]
then echo "ignored_deps is set ($ignored_deps)"
else echo "ignored_deps is not set"
fi
The ${ignored_deps+x}notation evaluates to xif $ignored_depsis set, even if it is set to an empty string. If you only want it set to a non-empty value, then use a colon too:
该${ignored_deps+x}表示法评估为xif$ignored_deps已设置,即使它被设置为空字符串。如果您只想将其设置为非空值,那么也可以使用冒号:
if [ ${ignored_deps:+x} ]
then echo "ignored_deps is set ($ignored_deps)"
else echo "ignored_deps is not set or is empty"
fi
If you want to execute the function (assuming the dashes work in the function name), then:
如果要执行该函数(假设破折号在函数名称中有效),则:
if ignored-deps-are-not-set
then echo "Function returned a zero (success) status"
else echo "Function returned a non-zero (failure) status"
fi
回答by glenn Hymanman
You're not actually executing the function:
您实际上并未执行该功能:
if ignored-deps-are-not-set; then ...
Withing []brackets, the literal string "ignored-deps-are-not-set" is seen as true.
使用[]括号,文字字符串“ignored-deps-are-not-set”被视为真。
回答by Kashyap
--edit-- just realized that it's a function tha tyou're trying to call, convention is wrong.
--edit-- 刚刚意识到这是您要调用的函数,约定是错误的。
See:
看:
Z000DGQD@CND131D5W6 ~
$ function a-b-c() {
> return 1
> }
Z000DGQD@CND131D5W6 ~
$ a-b-c
Z000DGQD@CND131D5W6 ~
$ echo $?
1
Z000DGQD@CND131D5W6 ~
$ if a-b-c; then echo hi; else echo ho; fi
ho
Z000DGQD@CND131D5W6 ~
$ if [ a-b-c ]; then echo hi; else echo ho; fi
hi
Z000DGQD@CND131D5W6 ~
--edit end--
--编辑结束--
Fix the variable name (see my comment to your post)
修复变量名称(请参阅我对您帖子的评论)
then
然后
See Parameter Expansionsection in man bash.
请参阅 中的参数扩展部分man bash。
${parameter:?word}:
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.
如果为空或未设置,则显示错误。如果参数为 null 或未设置,则 word 的扩展(或如果 word 不存在则显示该效果的消息)将写入标准错误,并且如果 shell 不是交互式的,则退出。否则,替换参数的值。
回答by phil
Yet another way to test for the existence of a variable:
另一种测试变量是否存在的方法:
if compgen -A variable test_existence_of_var; then
echo yes
else
echo no
fi
回答by Marc B
if [ ${myvar:-notset} -eq "notset" ] then
...

