:: Re: [DNG] Wifi device names: was sy…
トップ ページ
このメッセージを削除
このメッセージに返信
著者: Rainer Weikusat
日付:  
To: dng
題目: Re: [DNG] Wifi device names: was systemd is haunting me
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 :
----