I want to create an array in Bash which to contain all active network interfaces.
I have managed to create a for loop printing them but when I try to create the array, it just contains the last lo
interface but not the other. This is my code:
#!/bin/bash
for iface in $(ifconfig | cut -d ' ' -f1| tr ':' '\n' | awk NF)
do
printf "$iface%s\n"
declare -a array_test=["$iface"]
done
for i in "${array_test[@]}"; do echo "$i"; done
And this is my output:
eno1
eno2
eno3
lo
[lo]
Also, how can I exclude the lo localhost interface from the array?
bash
can be used for quite complex scripts (as can, e.g.,ksh
orzsh
)....but the more complex it is, the harder it is to read, understand, and modify - and the slower it becomes (e.g. a loop around forking several programs is abysmally slow). At that point, the time/effort cost of writing and maintaining a shell script greatly exceeds the cost of learning the basics ofawk
(orperl
, orpython
) and rewriting it in about 1/10th the lines of codes. If you already know one of those languages, then that point is reached much earlier.