Trying to create a dynamic set of Key-Value pairs through a series of for loops that basically pull from a JSON file to create a series of arrays that are then iterated through to define the key-value pairs.
The issue I am having is that I keep getting the following error when I run the script:
array[key]=value: command not found
The information it should be writing to the array looks correct, just gives me that error.
Here's how I am trying to add to the arrays.
$array[$key]=$value
Not quite sure if I am trying to do something that simply cannot be done.
I've tried a few variations of the script, all with the same "command not found" error.
Here is the full script as written at the moment:
#!/bin/bash
backupTargets=()
#currently active nodes
nodeArray=()
#add proxmox api token
pveTOK=FOO
get_nodes () {
#grab currently active nodes
curl -s -k -H "Authorization: PVEAPIToken=$pveTOK" https:///api2/json/nodes | jq . > nodes
#use jq to extract inital data from the curl command, remove returns with tr and use sed to clean things up to make it ready to be placed
#in an array
nodes=$(jq [.data[].node] nodes | tr -d '\n'| sed 's/\[//g; s/\]//g; s/ //g; s/,/ /g; s/\"//g')
nodeArray=($nodes)
}
declare_variables () {
for node in "${nodeArray[@]}"
do
declare -A $node
done
}
get_backupVMs () {
#some nested for loops that iterate over the nodes to create a dictionary that contains each vm and the associated tags
for node in "${nodeArray[@]}"
do
hosts=()
curl -s -k -H "Authorization: PVEAPIToken=$pveTOK" https:///api2/json/nodes/$node/qemu | jq . > $node
name=$(jq [.data[].name] $node | tr -d '\n'| sed 's/\[//g; s/\]//g; s/ //g; s/,/ /g; s/\"//g')
hosts+=($name)
for host in "${hosts[@]}"
do
tags=$(jq '.data[] | select(.name=='\"$host\"').tags' $node | sed 's/\[//g; s/\]//g; s/ //g; s/;/ /g; s/\"//g')
$node[$host]=$tags
done
done
}
get_nodes
declare_variables
get_backupVMs
array.$is a special character for the shell indicating parameter expansion. When you start a line with $, the first word is never interpreted as a variable assignement, but as a command. That's why you get the error command not found.