bash 如何测试变量是否存在并已初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27108058/
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
How to test if a variable exists and has been initialized
提问by ogs
I have to execute a function which has to to test if a variable has been correctly defined in the bash and must use its associated value.
我必须执行一个函数,该函数必须测试一个变量是否在 bash 中正确定义并且必须使用其关联值。
For instance, these variables are initialized at the top of the script.
例如,这些变量在脚本的顶部初始化。
#!/bin/bash
var1_ID=0x04
var2_ID=0x05
var3_ID=0x06
var4_ID=0x09
I would like to call the script named test as follows:
我想调用名为 test 的脚本如下:
./test var1
The current implemented function is:
目前实现的功能是:
function Get()
{
if [ "_ID" != "" ]; then
echo "here"
echo $((_ID))
else
exit 0
fi
}
I don't understand why I obtain here
even if I enter ./test toto
or something else.
我不明白为什么here
即使我进入./test toto
或其他东西我也会得到。
Do I need to use a specific command, such as grep
?
我是否需要使用特定的命令,例如grep
?
回答by fedorqui 'SO stop harming'
You probably want to use indirect expansion: ${!variable}
and then -n
to check if it has been defined:
您可能想使用间接扩展:${!variable}
然后-n
检查它是否已定义:
The indirect expansionconsists in calling a variable with another variable. That is, as the variable name may be changing, instead of saying $a
we say ${!var}
and var=a
.
在间接扩大包括呼吁与另一个变量的变量。也就是说,因为变量名可能会改变,而不是说$a
我们说${!var}
和var=a
。
$ cat a
var1_ID=0x04
var2_ID=0x05
var3_ID=0x06
var4_ID=0x09
for i in {1..5}; do
v="var${i}_ID"
if [ -n "${!v}" ]; then # <-- this expands to varX_ID
echo "$v set to value: ${!v}"
else
echo "$v not set"
fi
done
If we execute, we get:
如果我们执行,我们得到:
$ ./a
var1_ID set to value: 0x04
var2_ID set to value: 0x05
var3_ID set to value: 0x06
var4_ID set to value: 0x09
var5_ID not set
From man test
:
来自man test
:
-nSTRING
the length of STRING is nonzero
-n字符串
STRING 的长度非零
回答by choroba
Use Parameter expansion:
使用参数扩展:
: ${var:?}
Remove the colon if the empty string is a valid value (i.e. you only want to test for definedness).
如果空字符串是有效值(即您只想测试定义性),则删除冒号。
: ${var?}
If you don't want the script to stop on the problem, you can use
如果您不希望脚本在问题上停止,您可以使用
if [[ ${var:+1} ]] ; then
# OK...
else
echo Variable empty or not defined. >&2
fi
回答by chepner
In bash
4.2, you can use the -v
operator in a conditional expression to test if a variable with the given name is set.
在bash
4.2 中,您可以-v
在条件表达式中使用运算符来测试是否设置了具有给定名称的变量。
if [[ -v _ID ]]; then
echo "_ID is set"
foo=_ID
echo "${!foo}"
fi
You still need indirect parameter expansion to get the value.
您仍然需要间接参数扩展来获取值。
In bash
4.3 you can use a named reference to make working with it easier.
在bash
4.3 中,您可以使用命名引用来更轻松地使用它。
declare -n param=_ID
if [[ -v param ]]; then
echo "_ID"
echo "$param"
fi
(param
will behave exactly like the variable it references. I don't know if there is an easy way, short of parsing the output of declare -p param
, to get the name of the variable it references.)
(param
将表现得与它引用的变量完全一样。我不知道是否有一种简单的方法,除了解析 的输出之外declare -p param
,还可以获取它引用的变量的名称。)