bash与冒号的关联数组键字符串给出错误 [英] bash associative array key string with colon is giving error

查看:88
本文介绍了bash与冒号的关联数组键字符串给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建源和目标MAC地址的关联数组.

I am creating an associative array of source and destination MAC addresses.

$ declare -a SrcDstMap
$ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
-bash: 9c: value too great for base (error token is "9c")
$ SrcDstMap["fc:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
-bash: fc:4e:20:73:e2:72: syntax error in expression (error token is ":4e:20:73:e2:72")

我怎么能告诉bash给定的键是一个完整的字符串.

How can I tell bash that the given key is a whole string.

推荐答案

这不是一个关联数组.您需要使用declare -A,而不是declare -a.

That's not an associative array. You need to use declare -A, not declare -a.

$ declare -A SrcDstMap
$ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
$ declare -p SrcDstMap
declare -A SrcDstMap=([9c:4e:20:73:e2:72]="ff:ff:ff:ff:ff:ff" )


declare -a创建一个数字索引数组,因此shell尝试将索引解析为数字.


declare -a creates a numerically indexed array, so the shell is trying to parse your index as a number.

这篇关于bash与冒号的关联数组键字符串给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆