1

So I have this:

INTERFACES=($(ip -o link show | awk -F': ' '{print $2}'))

Giving me an array of interfaces, however I want to remove the loopback and tun adapters from the array.

INTERFACES=(${INTERFACES[@]/lo/})
INTERFACES=(${INTERFACES[@]/tun*/})

Works, but is there a way to do this in a single line?

Probably super easy and derp, but I'd like to understand how the expression actually works (google gave me 47239432 examples of the above but not one explained how it worked or gave an example of using more than one expression at a time).

I know this can be done with the awk command, so both a solution with awk and one with the array filter would be appreciated greatly :)

My apologies if this is a stupid question.

1 Answer 1

2

There are many ways to do this. One example:

# arr=(lo tun0 net1 net2)
# echo ${arr[@]}
lo tun0 net1 net2
# arr=( $( printf '%s\n' ${arr[@]} | egrep -v '^(lo|tun)' ) )
# echo ${arr[@]}
net1 net2
#

And you can also exclude these interfaces at the very beginning:

INTERFACES=( $(ip -o link show | awk -F': ' '$2 !~ /^(lo|tun)/ {print $2}') )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.