Re: adding leading zeros to a filename

From: Janis Papanagnou (Janis_Papanagnou_at_hotmail.com)
Date: 01/30/05


Date: Sun, 30 Jan 2005 21:10:40 +0100

alan@ljubljana.agtrz.com wrote:
> On 2005-01-28, Kevin Rodgers <ihs_4664@yahoo.com> wrote:
>>rupert wrote:
>>
>>>Basically I know nothing about shell scripting. I'm guessing I can
>>>write such a beast that will list the contents of a directory
>>
>>for file in *.jpg; do
>> ...
>>done
>>
>>or
>>
>>ls *.jpg | while read file; do
>>...
>>done
>
> How do you handle the case where directory contains no *.jpg.
>
> I get an error. Is there an idiom for the test?

ls *.jpg 2>/dev/null | while read file; do ... ; done

will not display an error and will not iterate the loop since the read
gets an EOF; this is often the desired behaviour. If you want a separate
handling for the error case you may catch the test before you enter the
loop as a separate command; test operators are [...] and [[...]].

Janis



Relevant Pages

  • Re: awks equiv to vis J
    ... Janis wrote: ... I'm sure I didn't invent it, I probably just picked it up somewhere, ... I've used that idiom at least in a posting back in 2007 (and mayve ... Actually (in the mentioned posting) this expression ...
    (comp.lang.awk)
  • Re: Event Invocation List
    ... the existing idiom: ... Delegate[] invocationList = ... Deriving the class and handling it internally ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: adding leading zeros to a filename
    ... > How do you handle the case where directory contains no *.jpg. ... Is there an idiom for the test? ... My code in this post is copyright 2005, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License ...
    (comp.unix.shell)
  • Re: lib for rotating jpegs
    ... > I'm searching for a free library for handling JPEG's. ... > want to load one JPG figure and just rotate it. ... After loading a JPG in memory, ...
    (comp.lang.c)