Re: adding leading zeros to a filename

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 01/30/05


Date: 30 Jan 2005 22:15:27 GMT

On Sun, 30 Jan 2005 at 19:37 GMT, 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?

    You can test before the loop:

set -- *.jpg
[ -f "$1" ] &&
  for file
  do
     : do whatever with "$file"
  done

     Or you can test within the loop:

for file in *.jpg
do
  [ -f "$file" ] || continue
  : do whatever with "$file"
done

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2005, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • 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? ... handling for the error case you may catch the test before you enter the ... Janis ...
    (comp.unix.shell)
  • Re: batch delete and rename files
    ... for file in *.jpg ## for each file enfing with .jpg ... ## remove leading zeroes, or the number will be read as octal, ... My code in this post is copyright 2004, Chris F.A. Johnson ...
    (comp.os.linux.misc)
  • Re: adding leading zeros to a filename
    ... 2005-01-30, 22:15, Chris F.A. Johnson: ... > You can test before the loop: ... symlinks to regular files which is likely to be the case for jpg ...
    (comp.unix.shell)
  • Re: Mass renaming of files
    ... Chris F.A. Johnson wrote: ... (SuSE 9.0) ... work in any shell where "for in *.jpg" works (which is any except ...
    (comp.os.linux.misc)