bug-fileutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ls: avoiding error msg on non-existant file


From: Karl O. Pinc
Subject: Re: ls: avoiding error msg on non-existant file
Date: Sun, 01 Dec 2002 17:57:12 -0600

On 2002.12.01 16:07 Bob Proulx wrote:
Karl O. Pinc <address@hidden> [2002-11-28 22:41:18 -0600]:

Hmm...  How about this suggestion below?

Yup, that's the right way.  Thanks.

> Here's the task at hand.  My pwd contains
> files named 'uN' where 'N' is an integral version number.  I want
> the last version number, if there is one.

Ew.  I am well versed with perl but not php.  I hear they are deeply
related.  So for now I will stick with shell for the examples.

I prefer php to perl, as perl's 12 different syntaxes for 1 semantic
feature overload my brain.  The downside of php is that it's got the
feel of having been written by amateurs: stuff like php's exec()
being Unix system(3) or "in-band/out-of-band" issues like forcing the value
of the the user supplied pathname to 'none' when the user declines
to upload a file.

> Really, I'm in search of an idiom to take the result of shell's
pattern
> expansion and pipe it somewhere, except the shell returns the
pattern
> itself when there's nothing in the fs which matches and I want
nothing
> at all in that case.

How about this as a possibility?  Still using 'ls'.  (I guess I could
have used 'echo * | tr " " "\012"' but I think this is more clear.)

The above 'echo' solution isn't general like the grep below either.

It uses grep to extract only matching patterns, adjust to fit your
need.  I don't think any errors would normally occur and so none need
to be ignored.

  last=$(ls | grep -E '^u[0-9]+' | cut -c 2- | sort -n | tail -n 1)

Doesn't use shell's pattern expansion, but I don't care.  It works.

I actually like the shell quite a bit for many tasks.  The shell is
both powerful and weak at the same time.  The art is to play to the
strengths.

I like shell too.  But I always wind up in quote hell, sooner or later.

I think that code like (bad example follows):

files="$(find . -name "$a $b")"

is the answer to my quote problems, but my mental model doesn't understand how that " nesting can work. And I think filenames with spaces break this
example, as they often break real code.  It's hard to write shell code
that works in the general case, even though it may work just fine in the
real-world case. And the problem is often with quoting. I hate having to settle for code that works in all expected cases but still contains a bug. Often I simply can't justify the time spent to get shell code really right.

The other failing of shell is the lack of an flock(), so there are often hidden
concurrency problems that are tough to avoid.  I keep meaning to write a
command line flock (that forks) but never get around to it. (I don't care
for 'file existance' locking conventions, and you can't block on file
existance in shell anyhow.)

Thanks for the help.

Karl <address@hidden>
Free Software:  "You don't pay back, you pay forward."
                 -- Robert A. Heinlein




reply via email to

[Prev in Thread] Current Thread [Next in Thread]