bash 如何检查变量是否为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14525296/
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 do I check if variable is an array?
提问by wujek
I have a loop over variable names and I need to check if content of a variable is an array or not:
我有一个变量名循环,我需要检查变量的内容是否是数组:
for varname in AA BB CC; do
local val
if [ "$varname" is array ]; then # how can I perform this test?
echo do something with an array
else
echo do something with a "'normal'" variable
fi
done
How do I do it?
我该怎么做?
采纳答案by Bastien Jansen
回答by Reuben W
To avoid a call to grep, you could use:
为避免调用 grep,您可以使用:
if [[ "$(declare -p variable_name)" =~ "declare -a" ]]; then
echo array
else
echo no array
fi
回答by Marco
Since bash 4.3 it is not that easy anymore.
从 bash 4.3 开始,它不再那么容易了。
With "declare -n" you can add a reference to another variable and you can do this over and over again. As if this was not complicated enough, with "declare -p", you do not get the type or the original variable.
使用“declare -n”,您可以添加对另一个变量的引用,并且可以一遍又一遍地执行此操作。好像这还不够复杂,使用“declare -p”,您不会获得类型或原始变量。
Example:
例子:
$ declare -a test=( a b c d e)
$ declare -n mytest=test
$ declare -n newtest=mytest
$ declare -p newtest
declare -n newtest="mytest"
$ declare -p mytest
declare -n mytest="test"
Therefore you have to loop through all the references. In bash-only this would look like this:
因此,您必须遍历所有引用。在 bash-only 中,这看起来像这样:
vartype() {
local var=$( declare -p )
local reg='^declare -n [^=]+=\"([^\"]+)\"$'
while [[ $var =~ $reg ]]; do
var=$( declare -p ${BASH_REMATCH[1]} )
done
case "${var#declare -}" in
a*)
echo "ARRAY"
;;
A*)
echo "HASH"
;;
i*)
echo "INT"
;;
x*)
echo "EXPORT"
;;
*)
echo "OTHER"
;;
esac
}
With the above example:
用上面的例子:
$ vartype newtest
ARRAY
To check for array, you can modify the code or use it with grep:
要检查数组,您可以修改代码或将其与 grep 一起使用:
vartype $varname | grep -q "ARRAY"
回答by miken32
I started with Reuben's great answerabove. I implemented a few of the comments and some of my own improvements and came out with this:
我从上面鲁本的精彩回答开始。我实施了一些评论和我自己的一些改进,并得出了以下结论:
#!/bin/bash
array_test() {
# no argument passed
[[ $# -ne 1 ]] && echo 'Supply a variable name as an argument'>&2 && return 2
var=
# use a variable to avoid having to escape spaces
regex="^declare -[aA] ${var}(=|$)"
[[ $(declare -p "$var" 2> /dev/null) =~ $regex ]] && return 0
}
Now I can do this:
现在我可以这样做:
foo=(lorem ipsum dolor)
bar="declare -a tricky"
declare -A baz
array_test foo && echo "it's an array"
array_test bar && echo "it's an array"
# properly detects empty arrays
array_test baz && echo "it's an array"
# won't throw errors on undeclared variables
array_test foobarbaz && echo "it's an array"
回答by Josef Ababat
Another Way:
其它的办法:
Example, create an Array:
例如,创建一个数组:
Variable=(The Quick Brown Fox...)
Test the Variable:
测试变量:
if [ "${#Variable[@]}" -gt "1" ] ;
then echo "Variable is an Array";
else echo "Variable is NOT an Array" ;
fi
回答by dawesh
is_array() {
local variable_name=
[[ "$(declare -p $variable_name)" =~ "declare -a" ]]
}
is_array BASH_VERSINFO && echo BASH_VERSINFO is an array
is_array() {
local variable_name=
[[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]]
}