0

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.

Varsh
  • 413
  • 9
  • 26
  • You say "is not as expected", but you don't describe, what exactly is the content of it, as opposed to what you hope would be stored in it. BTW, did you try to trace your code with `set -x`? – user1934428 Nov 09 '20 at 09:56
  • `[["${` is an error. Please check your scripts with shellcheck `[["${HOSTS[$j]}" = "${HOSTS[$i]}" ]];` - don't compare values, compare indexes. Just `if [[ "$i" != "$j" ]]` . The method is ok - it should work, only syntax errors are left for you to fix. And prefer `for i in "${!hosts[@]}"` to quote properly the indexes, and _prefer_ to use lowercase variables in your scripts, _not_ uppercase. As I'm curious, what system supports the `-I` argument to `hostname`? – KamilCuk Nov 09 '20 at 09:58
  • @Varsh: Your code should produce a _command not found_ error, but you don't mention it. Did you really post the exact code? – user1934428 Nov 09 '20 at 09:58
  • I edited the question please check. I didn't used `set -x` – Varsh Nov 09 '20 at 10:03
  • Yes, so read the duplicate. `[["${` is an error. Check your script with http://shellcheck.net – KamilCuk Nov 09 '20 at 10:06
  • Thank you @KamilCuk. The error is resolved. But where should I place `args+=("$CURRENTHOST") ` ? I need it in each set of array. I triend placing at the start and end of loop but it is not adding CURRENTHOST. – Varsh Nov 09 '20 at 10:19

0 Answers0