On Tue, Jun 10, 2008 at 6:34 PM, Douglas Royds <[EMAIL PROTECTED]> wrote:
>> ls -lh --time-style + $candidate | cut -d' ' -f9
> Cut seems to be vulnerable to the number of spaces:

Yes, it is, as the space is the field separator, and fields may be
null  -- which means it fails if there are spaces in the filename, but
I don't use them :-)
It will also fail if extra spaces are in the output of ls -l under
certain conditions ... but what are they?

>   $ ls -lh | cut -d' ' -f8
>             <--- This was the "total 1.5M" line

Yes, but I shouldn't have been cutting a -h line :-) because the
human-readable size output format changes under different conditions.
I should have left it as just -l.

>   $ ls -lh | cut -b50-

Well, only if the output you don't want is always the same size :-)
What if the username wasn't 'root' or the size ~1.1Mb?

> Was there meant to be something after the "+"?

According to the man page, --time-style takes a 'date' style +FORMAT
... and the format is null, which suppresses the output ... so
'--time- +' neatly drops the unwanted time field.

All this mucking around with ls is difficult, because the job of ls is
to produce human-readable output, and we're trying to turn it back in
to machine-readable. Probably better to use something like 'stat' in
the first place :-) except that 'stat -c %N' (which expands symlinks
for you) has an ugly output format ...however it can be sliced out
with one sed or two cut invocations, accurately.

candidate=$(stat -c %N $candidate|cut -d\` -f3|cut -d\' -f1)
or
candidate=$(stat -c %N /usr/bin/vi|sed -e "s/\`.*\`\(.*\)'/\1/")

Stat and ls are both in the Ubuntu coreutils package, so there should
be no problem with availability.

> PS: I resisted the urge to name the script "which, really?"

Mine is now called 'twhich' ... sort of "true which", also easy to
quickly add a t to the beginning of the previous command when I
remember I really want it :-)

Of course, all this would be generally much better done in a
programming language other than bash :-) but where's the fun in that?

-jim

Reply via email to