help-bash
[Top][All Lists]
Advanced

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

Re: Loop with files coming through sorted by name


From: Kerin Millar
Subject: Re: Loop with files coming through sorted by name
Date: Sat, 16 Oct 2021 06:38:14 +0100

On Sat, 16 Oct 2021 04:03:57 +0000
tolugboji <tolugboji@protonmail.com> wrote:

> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> 
> On Saturday, October 16th, 2021 at 3:31 AM, Seth David Schoen 
> <schoen@loyalty.org> wrote:
> 
> > Seth David Schoen writes:
> >
> > > Typically people will suggest instead using the find command, either
> > >
> > > with the -exec flag or with the -print0 flag. This is extensively
> > >
> > > described in
> > >
> > > https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find
> > >
> > > A disadvantage there is that the command run by the -exec can only be
> > >
> > > a single command, not a bunch of shell scripting or even a pipeline.
> >
> > I didn't realize, but there is a newer approach which involves a bunch of
> >
> > historically recent bashisms
> >
> > https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash/54561526#54561526
> >
> > You can thus do
> >
> > readarray -d "" myarray < <$(find . -type f -print0 | sort -zn)
> >
> > for i in "${myarray[@]}"; do
> >
> > arbitrary shell stuff with "$i"
> > ===============================
> >
> > done
> >
> > Classical Bourne shell scripting it isn't. :-)
> 
> 
> What's the opinion about
> 
>   for flimg in $( find $fdir type f -name "$fnam-*.png" | sort -n )
>   do
>     echo "$flimg"
>   done

Buggy in nature.

> 
> There is a number after "-" (e.g. edvart-01.png edvart-02.png ...)

If you are going to use sort -n, you must also guarantee that the number of 
digits in each name is the same. For instance, by specifying a pattern of 
"$fnam-[0-9][0-9].png". Otherwise, consider using -V to perform a natural sort, 
if available. Here is a solution using GNU find/sort.

while IFS= read -rd '' flimg; do
    printf '%s\n' "$flimg"
done < <(find "$fdir" -maxdepth 1 -type f -name "$fnam-*.png" -print0 | sort 
-zV)

-- 
Kerin Millar



reply via email to

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