Using 'find' and 'read' to traverse list, allowing user input inside the loop [Re: cd to folder with spaces - in a script]

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

 



On Fri, May 22, 2009 at 10:09 PM, Steven W. Orr <steveo@xxxxxxxxxxx> wrote:
> <snip>
> ii=0
> while read -d $'\000' FOLDERNAME
> do
>    savd=$PWD
>    cd "$FOLDERNAME"
>    ii=$((ii+1))
>    cd "$savd"
> done <(find . -type d -print0)

This is all cool, as long as you don't want user input in the middle
of that loop, using another 'read'. In order to do that, one can
redirect the outer pipe to eg. FD 3. The following is a otherwise
meaningless code to illustrate my point:

    #!/bin/bash
    mypattern='.directory'
    mylist=/tmp/mylist.txt
    #path="$1"
    path=$HOME
    while read -u 3 -d $'\000' i; do # see pipe after "done"
        echo "Found match: \"$i\""
        printf "Add to list ($mylist)? [Y|n] "
        read ans
        [[ x"$ans" == x"yes" || x"$ans" == x"y" || x"$ans" == x"" ]]
&& (echo "$i" >> $mylist && echo "Added $i") || echo "Skipped, moving
on..."
    done 3< <(find $path -iname "$mypattern" -type f -print0)

If I hadn't redirected the "find pipe" to FD 3, the inner "read ans"
would simply read the next matching filename, and fail the user input.

Not sure what would happen if FD 3 for some reason is occupied, maybe
the use of mkfifo is more robust, however I never found a way to get
around FD 3...:

    mypipe=`basename $0`-pipe
    [ -p $mypipe ] && (rm $mypipe;echo "removed stale pipe")
    mkfifo $mypipe
    find . -iname "$mypattern" -type f -print0 > $mypipe &
    while read -u 3 -d $'\000' i ; do # see pipe after "done"
    	...
    done 3< $mypipe
    rm $mypipe

best,
MartinG

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux