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

Volker Kuhlmann list0570 at paradise.net.nz
Sat Jul 9 11:36:40 NZST 2016


> Another option would be pure bash ...
> 
> bash -c "if [[ $env{ID_PATH_TAG} =~ (.)_._(.)$ ]]; then echo
> VCSOM_PORT_IDX=$(( (${BASH_REMATCH[1]} - 2) * 4 +
> ${BASH_REMATCH[2]})); fi"

Something like this would be preferable in my opinion, but I'd put it
into an external program (script). Quoting rules for udev may well be
messy (or buggy), calling a script is more legible and has no more
overhead than a 1-line inliner. In fact the above shell snippet is way
more performant than the sed pipelining in the other solution - bash
needs to be called in both cases, but calling sed is expensive in terms
of resources. Bash string handling comes at zero cost because it's
handled by bash itself. Did I mention legibility?

With a bash script you can also ensure that the script output is always
correct and as expected, with some kind of fallback. The sed line relies
on assumptions about its input.

Python lets you do all those things you can do with bash (and then
some), but cranking up python just to the point of the first code line
has a gigantic cost compared with bash. The script doesn't run often
though so you might find increased clarity preferable in the long run -
but beware language version and interpreter changes...

Volker

-- 
Volker Kuhlmann
http://volker.top.geek.nz/	Please do not CC list postings to me.


More information about the NZLUG mailing list