Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a file "in" that contains:

a
b
c

I run the following command:

cat in | while read el; do select yn in "yes" "no"; do echo $yn; done; done;

And get the following output:

1) yes
2) no
#?
#? 
#?

What I want is to be able to type 1 or 2 at each iteration. What do I need to do differently to make this work?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

You're piping input into a while loop. Within the loop, stdin comes from the preceding command, not from the console. Your select statement is reading from the same pipe that the read statement is reading from.

Also, you don't need cat.

You need to preserve stdin for use in the select statement. Try this:

while read el; do select yn in "yes" "no"; do echo $yn; break; 
done <&4; done 4<&0 < in

First, note that we're redirecting input using '<', rather than using cat.

This preserves your origin stdin (fd 0) in fd 4, and then inside the loop redirects input to select from fd 4.

share|improve this answer
    
Very cool, this definitely works. Is there a way to do it with a pipe? The reason being is that my original input comes from a series of commands instead of a flat file. I can definitely write the output to a file and redirect from the new file, but I'm curious how to do it with a pipe. –  Noah Campbell Dec 25 '09 at 19:49
    
Yeah, you can do the same thing with a pipe. In either case (using "<" or "|") you're just redirecting stdin. Try this: (cat input | while read el; do select yn in "yes" "no"; do echo $yn; break; done <&4; done) 4<&0 –  larsks Dec 26 '09 at 2:02
add comment

select will read an input and execute it repeatedly until EOF; what you almost certainly want is the superficially similar:

cat in | while read el; do select yn in "yes" "no"; do echo $yn; break; done; done
share|improve this answer
    
It still doesn't prompt me for the select and only presents the prompt twice. I'm still don't get the behavior I want. –  Noah Campbell Dec 24 '09 at 1:45
    
Works For Me (TM) –  womble Dec 24 '09 at 2:06
    
It doesn't work for me. I get all output at once and it doesn't wait for input. Similar to what the OP's version does, just sort of in a different order. –  Dennis Williamson Dec 24 '09 at 2:28
add comment

Your Answer

 
discard

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.