Re: ls: avoiding error msg on non-existant file
"Karl O. Pinc" <[email protected]>
| Newsgroups | gmane.comp.gnu.fileutils.bugs |
|---|---|
| Message-ID | <[email protected]> |
On 2002.11.28 17:10 Bob Proulx wrote: > Karl O. Pinc <[email protected]> [2002-11-22 11:22:05 -0600]: > > >> if [ ! -e . ] ; then ls pattern ; fi > > if [ -s . ] ; then ls pattern ; fi > > > > Don't know what I was thinking. > > You know how sometimes you say something and you know it is wrong but > other people just don't let it go by? This is one of those. :-) > Sorry but won't -s . always be true since . is a non-zero sized file > because all directories contain . and .. in them? Your right. And here I did some (obviously bogus) test and thought some miracle was occuring. Thanks for keeping me honest. > > And concerning your previous problem I really wanted to contribute > more to it. But I was only reading it at a 50% comprehension level. > But it seems that you have solved your problem already. Well, there is a solution, but I don't like it much. 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. Here's my code: last=$(for f in u* ; do [ -e "$f" ] && echo "$f" ; done | cut -c 2- | sort -n | tail -n 1 ) (Except This is a translation into pure shell. I'm writing in php and using php's exec() (which is really system(3)) instead of $() to produce the vaue for $last. And the real code abstracts out the magic numbers like the 'u' and the number 2. FWIW, php's exec() has an implicit 'tail -n 1' built in.) Writing the for loop seems like an awful lot of code. I used to have an 'ls u*' instead, but got errors when there were no (matching) files and so no shell pattern expansion. Hence my initial request to allow ls to ignore non-existant files. 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. In my case, the whole thing is somewhat academic. My pwd should always either be empty or contain files which match the pattern. But I figured that someday I might want to put a README file in there or something. (Maybe the next guy who comes along will do something stupid and put other stuff in the same directory.) I wouldn't want the code to break then. Oh well. It's shell. How elegent can it be made anyhow? Karl <[email protected]> Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein