[NZLUG] udev shell (or awk or ...) one-liner puzzle

Eliot Blennerhassett ewblen at gmail.com
Sun Jul 10 12:58:11 NZST 2016


Thanks Martin, Jim & Volker!

Martin's one liner works fine in a shell, but as Volker predicted, the
quoting/interpretation in udev (and my inability to figure out what if
any escaping would negate that) meant I have gone with an external script.

For my own understanding I took one of Martin's solutions apart:

# Relpace extraneous characters [!0-9] with nothing
digits=${1//[!0-9]/}

# $digits has leading zeros, so avoid octal interpretation by explict
decimal prefix
decimal=10#$digits

# use division to 'shift' digits, and % to extract
idx=$((decimal / 100 % 10 * 4 - 8 + decimal % 10))

echo VSCOM_PORT_IDX=$idx

==========
Thanks Jim for introducing me to bash regexp via =~ and BASH_REMATCH

I found that it works without the if/then statement:

> [[ "foo-0000_9_3_5_1" =~ (.)_._(.)$ ]]  ; echo PORT_IDX=$(( (${BASH_REMATCH[1]} - 2) * 4 + ${BASH_REMATCH[2]}));

On 09/07/16 01:40, Martin D Kealey wrote:
> Depending on whether you literally mean "digits after excluding all other
> characters",

Yes, literally digits, and with limited range of values [2-3]_?_[0-3]
that relate to the internal USB topology of the interface box.

>   x=${ID_PATH_TAG//[!0-9]/} ; x=10#${x:(-3)} ; echo VSCOM_PORT_IDX=$(( x/100 * 4 + x%10 - 8 ))


More information about the NZLUG mailing list