1

As shown below, first loop aims at reading steps from a text file while the second loop is intended to accept an option from user according to what user has seen on the screen, and do something, say configuring package.use or something else, and then go on executing the next step, until the end of steps.

The problem is the second while loop will neither show up options nor continuously read steps from text file, it just exits both of while loops after executing first step of steps, in this case, it shows up the result of executing emerge --pretend ceph and exits both of loops.

steps text file:

ceph
jdk
firefox
...

nested while loop:

#/bin/bash
STEPS="./steps"
while read -r line;
do
    if [[ ! $line = "#"* ]]; then
                emerge --pretend $line
                while read -p 'Please choose something:(1:package.use  2:package.license  3:package.keywords) ' input;
                do
                case $input in
                        1) echo "$input has been chosen." 
                           #set up package.use
                        ;;
                        ...
                esac
                done
        fi
done <$STEPS
7
  • Possible duplicate: Read input in bash inside a while loop. Commented Nov 19, 2015 at 18:03
  • Obviously, that thread is quite different from this one because what I'm asking is a nested while loop, and furthermore, my situation is not read something from a file or directory. What I'm asking is inside of while loop, there's another while loop which is intended to accept user's option. Commented Nov 19, 2015 at 18:07
  • It's still the same cause and the same effect. It doesn't matter that the other question doesn't have a nested while, what matters is that it is attempting to read from the user inside a read loop that's already been redirected. The solutions posted in that question will work for your problem as well. Commented Nov 19, 2015 at 18:09
  • As for your comment that "my situation is not something read from a file or directory", yes it is. Your outer loop is redirected from ./steps, and thus everything inside that loop (including the entire inner loop) also shares the same redirected I/O. Thus, your inner read is also reading from ./steps. Commented Nov 19, 2015 at 18:12
  • Thank you very much! It seemed my problem solved. I thought the reason why I have this problem is because of while loop. Thanks again. Commented Nov 19, 2015 at 18:21

1 Answer 1

0

The problem is you are redirecting stdin for the outer loop to come from your file, and when you try to do a read in the inner loop, it's also reading from that file.

See the following question for some possible solutions:

Read input in bash inside a while loop

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

4 Comments

If you have identified a duplicate, please create a comment to that effect. Once you have sufficient privileges, you will be allowed to actually vote to close a question as a duplicate (it takes five votes to close it, though gold badge users can do it by themselves).
Thanks. I'll do that. I wasn't sure what the correct protocol was.
The formal documentation is here: meta.stackexchange.com/help/privileges/close-questions ... But it doesn't address it from the perspective of somebody with less than 3,000 reputation. Maybe there is a meta question somewhere about that part.
No worries. Next time I'll just skip straight to adding a "duplicate" comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.