1

In a script, I'd like to read a process output line by line, and get a confirmation from the user. So far I've done this:

mycommand-outputpiped | while (read line)
do
   read line
   #dostuff

   read confirm #oops -> this read the next item from the pipe, not the keyboard
done

So I tried to add:

read confirm < /dev/stdin

But it didn't change the thing, it still reads the next line from the pipe... How should I handle this?

2
  • Not related, but why do you have a read command in both the condition list and the loop body for the variable line? Commented Mar 5, 2013 at 17:33
  • I'm not sure about that - I believe the first checks there are still datas to read while the second reads the datas. I'm not sure how to clean it up Commented Mar 5, 2013 at 17:35

1 Answer 1

9

Both read commands are reading from the standard input stream inherited from the while loop. The following should work; your second read needs to read directly from the terminal, not /dev/stdin (which is the pipe).

mycommand-outputpiped | while read line
do
    # do stuff
    read confirm < /dev/tty
done

Note that there is just one read, in the while condition, and that it is not enclosed in parentheses (which would create a subshell, and line would only be available in that subshell, not the loop body).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.