I try to run a command for a list of hosts and then store the output in a variable in a loop. I also created an array to associate ip/hostname as my command will only accept IP address as an argument but I want to use hostname and channel name in variable name. My code looks something like:
#!/bin/bash
IP="10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.0.5"
CHANNEL="1 2 3 "
USERNAME="username"
SCRIPT_HOST="myscript_host"
HOME_DIR="/home/myuser"
SCRIPT_DIR=$HOME_DIR/scripts
COMMAND="sudo /path_to_my_remote_script"
SSH="ssh -t -o ConnectTimeout=10 -l $USERNAME"
declare -A array
array[10.0.0.1]="host1"
array[10.0.0.2]="host2"
array[10.0.0.3]="host3"
array[10.0.0.4]="host4"
array[10.0.0.5]="host5"
for ip in ${IP} ; do
for channel in ${CHANNEL} ; do
my_variable_name_$(${array[$($ip)]})_$c=$($SSH "$COMMAND -i $ip |grep -i \"ipv4 count\"|awk {print \$4}'")
echo my_variable_name_$(${array[$($ip)]})_$c
done;done
When I execute my script I receive an error message like:
./test_array.sh: line 20: 10.0.0.1: command not found ./test_array.sh: line 20: array: bad array subscript
I can guess it's a syntax error but can't figure out. I appreciate any help.