Hi team....

I have been whipping up a wee bash script to empty out the photos of my
digital camera to my storage device. It runs from ivman when the camera
is plugged in and the idea was to have as little user intervention as
possible. It started off as a small bash script that was all right if
you were watching on a terminal, but not robust enough to run unsupervised.
I have come unstuck when trying to deal with CLI programs that expect an
input on stdin but give me a question on std{out/err} that has no linefeed.
I have found it very tricky indeed to read stdout when there is no LF.

Here is some example code:

#/bin/bash

# init some files and a pipe to play with
touch file{1,2}
mkfifo pipe1

catch_question()
{
# with this read loop I would like to read from stdin (the std{out,err}
from cp)
# problem is with the 'file exists, overwrite (y|n)?' type questions a
LF is often
# not sent in order the the user input occurs on the same line.
# hence 'read' seems unusable in this context ?
    while read LINE
    do
        # if I find something of concern then I will do my own UI for
the question?
        if echo "$LINE"|egrep 'overwrite.+?' > /dev/null
        then
            if kdialog --yesno "$LINE" # your choice of UI here
            then
                # and copy a response to the pipe for the blocked cp
                echo "y" > pipe1
            else
                echo "n" > pipe1
            fi
        fi
    done
}

# here I have a cp that complains of existing files
# stderr and stdout are combined and piped to 'catch_question'
# cp is expecting responses from the named pipe 'pipe1'

cp -iv file1 file2 2>&1 < pipe1 | catch_question

#clean up
rm file1 file2 pipe1



So, if anyone has some ideas about how to read 'incomplete' lines from
stdin or approaches to the task above I'd love some new ideas.
Meanwhile I'm off to 'man expect' - I may be some time!

: )
ChrisB

Reply via email to