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
Tao Wang
  • 186
  • 1
  • 18
  • Possible duplicate: [Read input in bash inside a while loop](http://stackoverflow.com/questions/6883363/read-input-in-bash-inside-a-while-loop). – Mike Holt Nov 19 '15 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. – Tao Wang Nov 19 '15 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. – Mike Holt Nov 19 '15 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`. – Mike Holt Nov 19 '15 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. – Tao Wang Nov 19 '15 at 18:21
  • You're quite welcome. – Mike Holt Nov 19 '15 at 18:22
  • BTW, the thread you mentioned, its solution is not clear, it may not be suitable for the beginner. What I've done to solve my problem is to add a pair of parenthesis right after the first while loop, `while ... do (commandlines....) – Tao Wang Nov 19 '15 at 18:24

1 Answers1

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

Community
  • 1
  • 1
Mike Holt
  • 4,452
  • 1
  • 17
  • 24
  • 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). – tripleee Nov 19 '15 at 18:02
  • Thanks. I'll do that. I wasn't sure what the correct protocol was. – Mike Holt Nov 19 '15 at 18:03
  • The formal documentation is here: http://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. – tripleee Nov 19 '15 at 18:06
  • No worries. Next time I'll just skip straight to adding a "duplicate" comment. – Mike Holt Nov 19 '15 at 18:07