Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a script (let's name it that calls some other scripts based on input parameters.

Finally, it calls the script requests user's input in case it find that some files already exists:

"Would you like to replace the configuration file with a new one? (Yes/No/Abort): "

Now, what I want to do it to simulate the keystroke of "Y"/"y" inside the script in order to always overwrite the files.

I cannot use expect.

How can I do that?

share|improve this question
How is getting the input? If simply reads from STDIN, then echo y | should do it. (If the question may occur multiple times, then yes y | – manatwork Feb 26 '13 at 8:20
@manatwork that's really an answer. I was about to post it but that would then called a copy-paste. :) – mtk Feb 26 '13 at 8:28
May be an answer, unless already passes input to, so reads user input from /dev/tty. – manatwork Feb 26 '13 at 8:32 call as follows: ... bla bla ... option – Peter Feb 26 '13 at 9:01
@Peter: man yes, read it – goldilocks Feb 26 '13 at 9:17

1 Answer 1

While yes | as manatwork already commented is a valid solution, from a programmers point of view I would patch to be able to handle this usecase.

Like add an --force option to not prompt but always overwrite files.

But to answer the subject of your question and to get something more expect-like and not only fire y via a pipe:



mkfifo ${fifo}

exec 3<> ${fifo}

expect="Would you like to replace the configuration file with a new one? (Yes/No/Abort): "

while IFS= read -d $'\0' -n 1 a ; do

    if [ "${str}" = "${expect}" ] ; then
        echo "!!! found: ${str}"
        echo ">>> sending answer: ${answer}"
        echo "${answer}" >&3
        unset str

    if [ "$a" = $'\n' ] ; then
        echo -n "--- discarding input line: ${str}"
        unset str
done < <(./ <${fifo})

rm ${fifo}

I just wrote that.. so it is not really failsafe or tested for solving specific problems.. So use at your own risk 8) Some line buffering problems may occur under some conditions..

But at least it worked in my test scenario..

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.