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]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux