:: Re: [DNG] Wifi device names: was sy…
Pàgina inicial
Delete this message
Reply to this message
Autor: Steve Litt
Data:  
A: dng
Assumpte: Re: [DNG] Wifi device names: was systemd is haunting me
On Sun, 31 Jan 2016 16:56:37 +0000
Rainer Weikusat <rainerweikusat@???> wrote:

> Steve Litt <slitt@???> writes:
>
> [...]
>
>
> > ========================================================
> > #!/bin/sh
> > if test "$#" == "0"; then
> > lineno="1"
> > else
> > lineno=$1
> > fi
> >
> > ip link | \
> > cut -d ' ' -f2 | \
> > grep ^w | \
> > sed -e "s/:\s*$//" | \
> > head -n $lineno | \
> > tail -n 1
> >
> > ========================================================
>
> The ip command has an -o flag for generating output which is more
> easily parseable by program (-o means 'oneline'). This means the same
> can be accomplished with
>
> ----
> #!/bin/sh
> lineno=${1:-1}
> ip -o link | awk "\$2 ~ /^w/{ if (++nr == $lineno) print
> gensub(\":\", \"\", 1, \$2) }" ----
>
> awk is generally the easiest to use tool if records made up of lines
> are supposed to be split into whitespace-separated fields. The code
> can be simplified somewhat by not using the awk gensub function to
> get rid of the trailing : but the tr command in 'delete characters'
> mode.
>
> ====
> #!/bin/sh
> lineno=${1:-1}
> ip -o link | \
> awk "\$2 ~ /^w/{ if (++nr == $lineno) print \$2 }" | \
> tr -d :

====

Nice! The lineno=${1:-1} is a huge improvement over my brute force 5
line if statement. Your version with tr is much more understandable
than your one with gensub: As a matter of fact, I wouldn't use the one
with gensub on the grounds that it's too complicated for someone not
familiar with awk on a programming level.

I'm debating whether to cut | grep | sed | head | tail with your awk |
tr. Mine has an unintended bug that no matter how high the argument, it
prints the "last" one, where as yours prints nothing if the argument is
higher. I think my unintended bug might be considered beneficial,
although even better would be that if it's too high to return the
number of the last wifi device.

Thanks,

SteveT

Steve Litt
January 2016 featured book: Twenty Eight Tales of Troubleshooting
http://www.troubleshooters.com/28