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

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

On Fri, May 22, 2009 at 10:09 PM, Steven W. Orr <steveo syslang net> 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:

    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
    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


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