1

I'm creating a script in Bash to change all MAC addresses of my PC. I can list all network interfaces with this:

ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo

And the output of the script is:

eth0
wlan0

Now I need to create a variable for each network interface (to use it in the future), but I don't know how, and Google didn't help me...

4 Answers 4

3

Answer:

readarray -t interfaces < <(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo)

echo "${interfaces[0]}" # prints eth0
echo "${interfaces[1]}" # prints wlan0


And to loop over them use for:

for curInterface in "${interfaces[@]}"; do
    echo "$curInterface"
done



But there are better ways to parse data:

First of all, instead of grepping < character you can use -o flag. This will output all of the data on single lines. Then you simply need the second word without : character. This is very simple in pure bash:

interfaces=()
while read -r _ curInterface _; do
    interfaces+=("${curInterface%:}")
done < <(ip -o link)
Sign up to request clarification or add additional context in comments.

11 Comments

Not quite enough. You forgot -t to readarray making it include the newlines on the array as well. Also, your other way to parse data would place it on a subshell leaving interfaces to no value.
Seems like it was your first attempt to use readarray? ;) I also suggest using other variables beside _ for the sake of good practice. Bash uses that variable as well.
@konsolebox of course not, I have simply forgot to write it because I was testing it in a terminal and out of laziness wrote echo ${curInterface[0]} without quotes. echo successfully trimmed the string and made me think that I'm done :) How does bash use _ variable?
I see. gnu.org/software/bash/manual/html_node/Special-Parameters.html -> I guess it's just an option to override the variable. I prefer using __. Actually I'm probably the only one who does that, unless someone copies my style :)
Thanks fou your help, but when I add 'readarray -t interfaces < <(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo)' the shell give me a error: 'Syntax error: redirection unexpected' .... why¿? :(
|
0

Store the output in an array:

interfaces=( $(ip link | awk '/</ { print $2 }' | awk -F: '!/lo/ {print $1}') )

Comments

0

You can create an array from this output, and loop through it after.

my_array=( $(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo) )

You can also this exmaple giving different alternatives redirect output to array

Comments

0

And I could have it simpler like this with one awk command:

readarray -t youravar < <(exec ip link | awk -F': ' '/^[0-9]+:/&&!/ lo: /{print $2}')

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.