'how to traverse through a for loop while using a sudo command to grab strings in bash

Code is shown below for my function, seems like I am having an issue with traversing through the for loop delimeter:

print_labels(){
        typeset -p target_nodes port name node_count management
        for x in "${target_nodes[${port}]}"; do
                while [[ $port -le $node_count ]]
                do
                        if (( $management == 1 )); then
                                printf "$x-man0\n"
                                printf "%s p%d\n\n" $name $port
                                let "port=port+1"
                                shift;
                        elif (( $management == 0 )); then
                                printf "$x\n"
                                printf "%s p%d\n\n" $name $port
                                let "port=port+1"
                                shift;
                        else
                                printf"\nPRINT ERROR"
                        fi
                done
        done
}

With the use of typeset the values of the variables are given below, and the output looks like this shown below:

declare -- target_nodes="node2005

node2006

node2007"

declare -- port="0"

declare -- name="bx42"

declare -- node_count="3"

declare -- management="1"

node2005

node2006

node2007-man0

bx42 p0

node2005

node2006

node2007-man0

bx42 p1

node2005

node2006

node2007-man0

bx42 p2

node2005

node2006

node2007-man0

bx42 p3

where ideally i want one node in each block, going from 2005 to 2007, like this below

node2005-man0

bx42 p1

node2006-man0

bx42 p2

node2007-man0

bx42 p3

Any help would be greatly appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source