Hello and thank you for your time. So, I have been coding a script to get the information from switchs in a local network, however i have met this strange behavior in Bash of which i was not aware and i don't seem to find anything on:
#!/bin/bash
SWITCH1=../SwitchData/switch.txt
p_vlan=($(cut -d ' ' -f1 $SWITCH1))
p_mac=($(cut -d ' ' -f2 $SWITCH1))
p_port=($(cut -d ' ' -f4 $SWITCH1))
for ((i=0;i<"${#p_port[@]}";i++))
do
echo "${p_port[$i]}"
echo "${p_port[$i]} ${p_mac[$i]} ${p_vlan[$i]}"
done
echo "${p_port[@]}"
returns:
Fa0/1
b462.6c72.bae4 247
Fa0/1
9200.0b00.0d37 3
Fa0/1
a562.6a32.bae4 3
Fa0/3
a562.6a62.c73b 247
...
Fa0/22 0120.0b00.1950 6 Fa0/22 #from echo "${p_port[@]}"
so as we can see, when ${p_port[$i]} is written with other arrays, it is not written anymore. however, when written alone, the values still appears. also, echo "${p_port[@]}" which should write the whole array only write it's last value. and finally, when writing an equality with another value that should have the exact same value, it returns false, even though it should return true. so if you have any idea as to what might cause this issue, feel free to share your insight.
I must also add some information on the switch.txt file, in fact i have used the command:
sed -e "s/^M//g" ../SwitchData/switch.txt > ../SwitchData/temp.txt
cat ../SwitchData/temp.txt > ../SwitchData/switch.txt
or
sed -e -i "s/^M//g" ../SwitchData/switch.txt
In order to delete the ^M at the end of the file.
Also, changing "s/^M//g" by "s/^M/ /g" correct the issue.
PS: if you have any idea of a title that would be more relevant.