bash associative array key string with colon is giving error

bash unix associative-array

538 просмотра

1 ответ

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")

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

Автор: Necktwi Источник Размещён: 12.11.2019 09:28

Ответы (1)

4 плюса


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 creates a numerically indexed array, so the shell is trying to parse your index as a number.

Автор: Charles Duffy Размещён: 03.11.2016 04:03
Вопросы из категории :