Given a line like 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq_codel state UP group default qlen 1000
or 2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
How do I extract the interface name or more precisely all the non-space characters following “2:” up to the next “:” and then stop so I dont bring in the MAC address on the next line?
Asking for a friend who has never got his head around sed, awk and friends…
I could do
ip a >/tmp/ipa.txt
grep "2: " /tmp/ipa.txt|cut -f2 -d':'|cut -c2-
but this seems very kludgey
however it does seem to produce the output I need.
Btw, you don’t need that intermediate text file. You can pipe the output of ip a straight into grep.
Also, the $(...) notation is preferable to backticks now. I can’t remember exactly where I read that, but pretty sure I did. Plus, imo it’s just nicer to read.
Sed, awk, xargs etc all look terrifying. I watched a video which took away a lot of the scary voodoo (but of course I forgot it straight away). There will always be a market for the likes of @chriso who have ventured into the heart of darkness and emerged with the secret
@davidpbrown is another who knows this stuff inside out. I once thought I understood simple regexes but unless you are using them regularly, its not the kind of knowledge I can retain too well.
ip link show | sed -n '/^[0-9]/{s/^[0-9]*: \(.*\):.*$/\1/;p}'
or, if you only want interface 2 for some reason
ip link show | sed -n '/^2:/{s/^[0-9]*: \(.*\):.*$/\1/;p}'
or you can use a different output format that’s easier to parse , like
ip -oneline link show | sed -e 's/^[^:]*: *//' -e 's/:.*$//'
or
ip -brief link show | awk '{print $1;}'
or, if you want to get all the interface names without having to parse anything, withouth write-only regular expression code, and without forking
(cd /sys/class/net; echo *)
… but the real reason I’m posting an answer is to suggest that about the time you get to the point of parsing stuff like that, it’s a good time to switch to another programming language. Complicated shell scripts are the path of madness. Python isn’t great, but it’s a lot easier to write a working, relatively complicated program in a language like Python than in a shell.
On edit: oops, that does fork. But it doesn’t do an exec…
but as you say by the time we need to get more network info, its time for Python or even Rust.
Next I want to make the node connection info configurable so its easy to switch between @Josh@folaht and the official testnets. With configureable vault sizes, I think thats all we need for now. This is just to get a few more lurkers comfortable enough to join in.
Then create a similar script for Windows, Mac and non-Debian distros to get more folk involved
For sure, this is the main purpose. I may be a semi-n00b, but it’s also for people like me who don’t like copying or typing long command strings, just because it’s easy to make silly mistakes that way.
If we all do exactly the same thing, I also think the tests may turn out to be more informative, as well as require less hand-holding.