I have a array of IP addresses. While traversing the array, for each element in the current iteration, I need rest of the elements in the new array. I have tried this:
create_nodes_directories(){
read -r CURRENTHOST _ < <(hostname -I)
HOSTS=(192.168.110.165 192.168.110.166)
for i in ${!HOSTS[*]} ; do
#Here I need array elements other than ${HOSTS[$i]}
#created new array args()
args=()
for j in ${!HOSTS[*]} ; do
if [["${HOSTS[$j]}" = "${HOSTS[$i]}" ]]; then
continue;
else
args+=("${HOSTS[$j]}")
fi
done
args+=("$CURRENTHOST")
create_genesis_start_file ${args[*]}
done
}
For each iteration I need array elements other than ${HOSTS[$i]}. I have tried to create new array args
, compare the elements and adding rest of the array elements of HOSTS
. create_genesis_start_file
is the function whom I want to pass args
. This code giving error while comparing if [["${HOSTS[$j]}" = "${HOSTS[$i]}" ]]; then
and also the args array is not as expected.
Error:
environment: line 79: [[192.168.110.165: command not found
environment: line 79: [[192.168.110.166: command not found
Expected output example:
if array HOSTS is,
HOSTS=(192.168.110.165 192.168.110.166 192.168.110.167)
for the first iteration, I need
args=(192.168.110.166 192.168.110.167)
for second iteration,
args=(192.168.110.165 192.168.110.167)
and so on.
CURRENTHOST should be added always.
Please correct me.