[Toybox] I aten't dead.

Rob Landley rob at landley.net
Thu Feb 26 04:26:19 PST 2015


On 02/25/2015 10:27 AM, Isaac Dunham wrote:
> 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.

Which is why fstype exists, although it only gives you the type not the
other attributes.

> 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,

Which is why I didn't implement them. (That and the lack of standard.)

> 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"

If it takes a comma separated list and maybe I can get it to share code
with ps -o and mount -o.

(I'm currently filing rough edges off of ps, not yet a proper cleanup
but working my way towards it. I want to implement the bsd ps option
behavior but haven't figured out a graceful way to do it yet. Unlike
with tar, the options mean different things in the different contexts...)

> 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

I notice each field of that is on its own line, and unquoted by default.

> -o value uses unquoted strings like this:
> blkid -o value -s UUID -s LABEL /dev/sdb
> HA"; `rm -rf /`
> 282e77a6-cbd3-4692-8c62-715c45970377

The previous strings were unquoted (although their contents were
escaped), these are unlabeled. (And unescaped.)

> -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;

I note I'm in the process of upgrading mdev so I can promote that, but
there's a lot of work to do there too.

> this is needed for mount by label to work.

I've never understood why anyone does that, but it's not my place to
second guess people's use cases...

> And the ID_FS_LABEL_ENC field of -o udev matches what I'm looking for
> exactly.

I'm confused: what is your use case?

In theory properly quoted shell strings shouldn't care what their
contents are (modulo NUL bytes and stripping trailing whitespace):

  X="$(fruitbat -x)"
  command "$X"

Should be fine about having semicolons and quotes and stuff in what $X
expands to...? So the classic definition of "scriptable" would be not
having any LABEL= at all, but instead having a way to request the
specific field of interest...?

(And if you want a label that's really screwing with mount-by-label, put
a / in it.)

> Thanks,
> Isaac Dunham

Rob

 1424953579.0


More information about the Toybox mailing list