“无效算术运算符"外壳 [英] "invalid arithmetic operator" in shell
本文介绍了“无效算术运算符"外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cat test.sh
cat test.sh
#!/bin/bash
key="index";
arr[$key]="val"
echo ${arr[${key}]}
/bin/bash-x test.sh
/bin/bash-x test.sh
+ key=index
+ arr[$key]=val
+ echo val
val
然后我修改test.sh:
then I modify the test.sh:
#!/bin/bash
key="index.index";
arr[$key]="val"
echo ${arr[${key}]}
/bin/bash -x test.sh
/bin/bash -x test.sh
+ key=index.index
+ arr[$key]=val
test.sh: line 3: index.index: syntax error: invalid arithmetic operator (error token is ".index")
test.sh: line 4: index.index: syntax error: invalid arithmetic operator (error token is ".index")
为什么会出现此错误,任何建议将不胜感激!
why this error appears, any suggestion will be appreciate!
推荐答案
使用declare -A arr
将数组变量声明为关联数组.
Declare the array variable as an associative array with declare -A arr
.
$ cat test.sh
#!/bin/bash
set -x
declare -A arr
key="index.index";
arr["$key"]="val"
echo "${arr["${key}"]}"
$ ./test.sh
+ declare -A arr
+ key=index.index
+ arr["$key"]=val
+ echo val
val
这篇关于“无效算术运算符"外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文