[Toybox] I aten't dead.
Isaac Dunham
ibid.ag at gmail.com
Wed Feb 25 08:27:56 PST 2015
On Wed, Feb 25, 2015 at 03:19:23AM -0600, Rob Landley wrote:
> There's a marvelous book called "the unix philosophy" by mike gancarz
> that talks about output intended for humans vs output intended to be
> processed by other tools.
>
> A scriptable tool is one where you can do:
>
> echo $(( $(wc -l < blah) * 3))
>
> If it's scriptable, the output of one tool is naturally the input to
> another. Admittedly this can be annoying to use from the command line
> such as the way "ls" with no arguments will produce no output when run
> in an empty directory. But the _reason_ for that is so "for i in $(ls);
> do blah $i; done" doesn't have to filter the ls output to use it in a
> script.
Thread hijack/blkid feature I'd like to see (no need to do it now, I'm
willing to try writing it at a later point).
I was just fighting with non-scriptable output in another tool:
blkid.
By default it's ROUGHLY shell-quoted, with fields in variable positions
(sample is not actual output, but how it looks):
/dev/sda1: SEC_TYPE="msdos" LABEL="ECS_CDRUN" UUID="DEAD-BEEF" TYPE="vfat" PARTUUID="12345678-01"
/dev/sda2: UUID="01234567-abcd-bcde-cdef-0123456789ab" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="12345678-02"
/dev/sda3: UUID="01234567-abcd-bcde-cdef-0123456789ab" TYPE="ext4" PARTUUID="12345678-03"
/dev/sdb: LABEL="HA\"; `rm -rf /`" UUID="12345678-dcba-bcde-cdef-0123456789ab" TYPE="ext4"
That's how it does the quoting: "" around each field, \ before " in the label.
util-linux blkid has a whole bunch of rarely used options, but two of them
seem to make it a whole lot easier to parse:
-o <format> output format; can be one of:
value, device, export or full; (default: full)
(They don't mention -o udev here, but it works.)
-s <tag> show specified tag(s) (default show all tags)
<tag> is TYPE/SEC_TYPE/UUID/PARTUUID/LABEL, and means that only
the fields specified get shown, in the same order as usual.
Example:
blkid -s TYPE -s LABEL -s UUID /dev/sda1
/dev/sda1: LABEL="ECS_CDRUN" UUID="DEAD-BEEF" TYPE="vfat"
Of the listed output formats,
-o device prints the block device/file and nothing else,
-o export shell-quotes everything:
/sbin/blkid -s LABEL -s UUID -o export /dev/sdb
DEVNAME=/dev/sdb
LABEL=HA\";\ \`rm\ -rf\ /\`
UUID=282e77a6-cbd3-4692-8c62-715c45970377
-o value uses unquoted strings like this:
blkid -o value -s UUID -s LABEL /dev/sdb
HA"; `rm -rf /`
282e77a6-cbd3-4692-8c62-715c45970377
-o udev (which uses \x<ff> style quoting) is also available,
but only mentioned in the manpage.
Example:
/sbin/blkid -o udev test.img
ID_FS_LABEL=HA____rm_-rf_/_
ID_FS_LABEL_ENC=HA\x22\x3b\x20\x60rm\x20-rf\x20\x2f\x60
ID_FS_UUID=282e77a6-cbd3-4692-8c62-715c45970377
ID_FS_UUID_ENC=282e77a6-cbd3-4692-8c62-715c45970377
ID_FS_TYPE=ext2
I'm trying to parse this so I can get links in /dev/disk/by-label/
that match what udev produces; this is needed for mount by label to work.
And the ID_FS_LABEL_ENC field of -o udev matches what I'm looking for
exactly.
Thanks,
Isaac Dunham
1424881676.0
More information about the Toybox
mailing list