'printf changes for iteration over array

I am playing with a simple bash script to pull some interface info from network equipment using SNMP.

I store a list of IPs in an array and iterate over it to fetch and print data for each device.

The first iteration for ip1 looks all good, for ip2+ the output is messed up with a line break as shown below:

Interface information for SomeHostname on IP 10.10.10.10
 _______________________________________________________________________________________________________________                                                                                  
| Interface            | IP-Address/Prefix    | Status       | Interface type       | Last changed              |
| ____________________ | ____________________ | ____________ | ____________________ | __________________________|
| Gi0/0/1.100          | 1.2.3.4/24           | up(1)        | l2vlan(135)          | 157 d 16 h 28 m and 42 s  |
| Gi0/0/1.101          | 2.3.4.5/24           | up(1)        | l2vlan(135)          | 157 d 16 h 28 m and 42 s  |
| Gi0/0/1.102          | 3.4.5.6/24           | up(1)        | l2vlan(135)          | 143 d 7 h 39 m and 15 s   |
etc.

Interface information for SomeHostname on IP 10.10.10.11
 _______________________________________________________________________________________________________________                                                                                  
| Interface            | IP-Address/Prefix    | Status       | Interface type       | Last changed              |
| ____________________ | ____________________ | ____________ | ____________________ | __________________________|
| Gi0/0/1.100          | 4.5.6.7/24           | up(1)        | l2vlan(135)          | 746                       |
| d                    | 17                   | h            | 3                    | m                         |
| and                  | 26                   | s            |                      |                           |
etc.

The code printing this looks something like this:

for ip in "${ips[@]}"
do

printf "\nInterface information for $hostname on IP $ip\n"
printf "%-20s %-20s %-12s %-20s %-25s \n" " _______________________________________________________________________________________________________________"
printf "| %-20s | %-20s | %-12s | %-20s | %-25s |\n" "Interface" "IP-Address/Prefix" "Status" "Interface type" "Last changed"
printf "| %-20s | %-20s | %-12s | %-20s | %-25s|\n" "____________________" "____________________" "____________" "____________________" "__________________________"
                              
for ((i=0; i<${#ipAdEntAddr[@]}; i++)); do                              
    if_lastChange[$i]=$(snmpwalk -v2c -c $snmp_community $cpe 1.3.6.1.2.1.2.2.1.9.${ipAdEntIfIndex[$i]} | cut -d "(" -f2 | cut -d ")" -f1)
    uptime=$(snmpget -v 2c -c $snmp_community $cpe 1.3.6.1.6.3.10.2.1.3.0 |  sed -e 's/SNMP-FRAMEWORK-MIB::snmpEngineTime.0 = INTEGER: //g' | awk -F" " '{print $1}')
    iflastChange[$i]=$(echo $((${if_lastChange[$i]}/100)))
    ifc1[$c]=$(echo $((($uptime-${iflastChange[$i]}))))
    ifc2[$c]=$(displaytime $ifc1)

    printf "| %-20s | %-20s | %-12s | %-20s | %-25s |\n" ${ifDescr[$i]} ${ipAdEntAddr[$i]}"/"${ipAdEntNetMask[$i]} ${ifOpStatus[$i]} ${ifType[$i]} ${ifc2[$c]}
done
printf "%-20s %-20s %-12s %-20s %-25s \n" " _______________________________________________________________________________________________________________"
done

Can somebody point me to what I am doing wrong here?



Sources

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

Source: Stack Overflow

Solution Source