Help With 'for' Loop

David Kirchner dpk at dpk.net
Fri Oct 14 12:52:06 PDT 2005


On 10/14/05, Drew Tomlinson <drew at mykitchentable.net> wrote:
> Sorry to be such a pest today.  I'm working on a sh script that uses a
> for loop.  To test, I've written the following:
>
> for i in `/usr/bin/find /multimedia/Pictures -iname "*.jpg" -or -iname
> "*.gif" -print`
>         do
>         echo -e "\n$i"
> done
>
> The first line 'find' returns is "/multimedia/Pictures/1998
> Christmas/April01.JPG"
>
> Yet 'echo $i' only returns "/multimedia/Pictures/1998", stopping at the
> first space.  Is it possible to get 'i' to represent the whole string
> that 'find' returns?  If so, how?

'while read i' will do what you want, but may cause issues with
programs that expect to be able to read from stdin within the loop.

find $findstuff | while read i
do
    echo $i
done

You can also try something like:

find $findstuff -exec echo {} \;

(where $findstuff is your -iname conditionals). {} is replaced by the
files or directories found by find, and \; is necessary to terminate
the -exec argument.


More information about the freebsd-questions mailing list