View previous topic :: View next topic |
Author |
Message |
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Sun Mar 16, 2014 7:27 pm Post subject: Case Conversion in UDEV Rules? |
|
|
In setting up a set of rules to mount attached removable media with a name equivalent to the volume label, I ran into an unexpected snag. I wanted to convert the volume label on dos/vfat media to lower case. This doesn't appear to be possible natively in a UDEV rule. It appears that the tool that UDEV rules typically use to get information about a volume is "/sbin/blkid", typically used like so: Code: | IMPORT{program}="/sbin/blkid -o udev -p %N" | Instead of running it directly in the rules, I created a wrapper script that translates some information to lower case: /usr/local/bin/modified-blkid.sh: | #/bin/sh
/sbin/blkid -o udev -p "${1}" | sed 's/\(ID_FS_LABEL=\)\(.*\)/\1\L\2/' | and then referenced it in my UDEV rules like so: Code: | IMPORT{program}="/usr/local/bin/modified-blkid.sh %N" | The documentation of udev string formation syntax is pretty sparse so I don't know whether there's a better way. At least sed runs fast. Technical review, please?
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Mar 22, 2014 4:29 pm Post subject: |
|
|
Polite bump.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
khayyam Watchman
Joined: 07 Jun 2012 Posts: 6227 Location: Room 101
|
Posted: Sat Mar 22, 2014 10:56 pm Post subject: |
|
|
John R. Graham wrote: | Technical review, please? |
John ... its terrrrrible ... you're using sed ... worse ... you're not using awk :)
Code: | /sbin/blkid -o udev -p "${1}" | awk -v FS="=" '/ID_FS_LABEL/{print $1"="tolower($2)}' |
... but seriously, I don't see any other way of doing it ... I'm pretty sure, from my admittedly sparse knowledge of the subject, udev doesn't provide a method for the conversion of strings.
best ... khayawk |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Sun Mar 23, 2014 11:40 am Post subject: |
|
|
What's even worse is that I did it on purpose.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
khayyam Watchman
Joined: 07 Jun 2012 Posts: 6227 Location: Room 101
|
Posted: Sun Mar 23, 2014 12:33 pm Post subject: |
|
|
John R. Graham wrote: | What's even worse is that I did it on purpose. |
John .... aha! ... you fell for my carefully laid trap, nobody uses sed "on purpose", they do so only after having seen it used elsewhere and convincing themselves that "that's just the tool for the job".
best ... khay
ps. '{gsub(/sed/,"awk")}' above.txt && echo ":)" |
|
Back to top |
|
|
steveL Watchman
Joined: 13 Sep 2006 Posts: 5153 Location: The Peanut Gallery
|
Posted: Mon Mar 24, 2014 8:54 am Post subject: |
|
|
If sed can do it, use sed, since it's faster than awk.
Having sed that, the awk will be more portable, unless \L is in POSIX (it's a new one on me, if it is, but I'd be more than happy if it were. :) So on non-GNU, or for ease of use across Gentoo installs, the awk wins (assuming \L is a GNUism.)
I'm not sure I'd do it how khayyam writes it.
Code: | /sbin/blkid -o udev -p "$1" | awk -v FS="=" '$1 == "ID_FS_LABEL" { print $1"="tolower($2); next } 1' | would appear to be better in efficiency terms (assuming blkid is not going to stick spaces on the LHS of =, if so use $1 ~ /^ *ID_FS_LABEL *$/ ).
Note you need 1 to print other lines, or awk will swallow them as not-matching.
I don't like the presumption that only the second field is required. It's not very robust (nor is assuming you're not going to get ID_FS_LABEL in the rest of the line: assume malicious data instead.)
Code: | /sbin/blkid -o udev -p "$1" | awk 'substr($0, 1, 12) == "ID_FS_LABEL=" { printf("ID_FS_LABEL=%s\n", tolower(substr($0, 13)) ); next } 1' | is what I'd use in awk.
printf tends to be faster than string concatenation with print. If you were doing it for more than one type of thing (or spaces might appear on left), you'd use
index($0, "=") + 1 as opposed to 13. I just use a fixed number, if it's a fixed string.
If you were doing it for more than one, the condition is equivalent to: /^ID_FS_LABEL=/ which many in #awk would prefer; I prefer not relying an the awk implementation to spot the equivalence. I would of course use a regex such as /^ID_FS_LABEL(_ENC)?=/ (and index) for more than one.
I use fixed stuff as opposed to field-splitting, as it's less work; awk doesn't field-split until it's required, at least in the one true awk code, so I'd assume others have the optimisation too.
I reckon I'd still use the sed where it works though (the y cmd can be hairy here, as well as non-locale aware), and also I hope you're limiting it to only apply to certain drives, jrg. |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Mar 24, 2014 2:30 pm Post subject: |
|
|
steveL wrote: | Having sed that, the awk will be more portable, unless \L is in POSIX (it's a new one on me, if it is, but I'd be more than happy if it were. So on non-GNU, or for ease of use across Gentoo installs, the awk wins (assuming \L is a GNUism.) | \L is indeed a GNUism, but since udev itself is a GNUism, I'm comfortable with it. Also, although all three of us know that all three of us are facile with sed, AWK, and Perl, note: Code: | ~ # for i in /bin/sed /usr/bin/gawk /usr/lib/libperl.so.5.16.3 ; do ls -l $i; done
-rwxr-xr-x 1 root root 59,484 Nov 15 2012 /bin/sed
-rwxr-xr-x 1 root root 387,880 Jan 7 13:48 /usr/bin/gawk
-rwxr-xr-x 1 root root 1,443,260 Dec 16 10:22 /usr/lib/libperl.so.5.16.3 | For this particular application, I chose the smallest, fastest tool on purpose.
And, yes, other udev rules restrict the renaming to USB media.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
steveL Watchman
Joined: 13 Sep 2006 Posts: 5153 Location: The Peanut Gallery
|
Posted: Mon Mar 24, 2014 3:14 pm Post subject: |
|
|
Yes, that's why I said: "If sed can do it, use sed, since it's faster than awk," and "I'd still use sed." ;-)
You're right though that udev is hardly portable to non-GNU. |
|
Back to top |
|
|
khayyam Watchman
Joined: 07 Jun 2012 Posts: 6227 Location: Room 101
|
Posted: Mon Mar 24, 2014 7:03 pm Post subject: |
|
|
John, steve ... you're both right ... but for the wrong reasons ... which is the worst kind of being right there is :)
The clue is right there: "but seriously [...]". I didn't imagine for a second that this "technical review" might include his using sed, all that was a preamble for what followed ... and I stuck with it for no other reason than John insisted on providing his (na-na-na-na-na ... *spurious*) reasons (yeah, you hurd me!). Next time I'll suggest javascript ... no, you deserve it, visual basic ... you gnuobules! :)
ta, ta ... khay |
|
Back to top |
|
|
steveL Watchman
Joined: 13 Sep 2006 Posts: 5153 Location: The Peanut Gallery
|
Posted: Tue Mar 25, 2014 5:47 am Post subject: |
|
|
Lol, no we got that you were joking; it's just not why we were discussing what we were discussing.
"We are geek!" ;) |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|