2

While being in loop, I am reading user input as below:

#!/bin/bash
while read line; do
    echo $line
    python find_entity.py $line
    read -p "Enter your name : " choice
    echo $choice
done < "q0.txt"

but still it does not ask for user input choice, rather writes True and just continue. What is wrong here?

4
  • 1
    Change the shebang to #!/bin/bash -x and look at the output... Commented Nov 20, 2014 at 11:26
  • @FredrikPihl: thanks but still now change Commented Nov 20, 2014 at 11:36
  • The purpose of the -x flag is to turn on debugging so that you'll see what your parameters expands to. Look at the output and verify that $line etc contains the expected values. Commented Nov 20, 2014 at 11:42
  • possible duplicate of Bash user prompt while reading file Commented Nov 20, 2014 at 11:46

2 Answers 2

2

The following will not cover all the possible use cases, but please try if this simple solution suffices for yours

...
read -p "Enter your name : " choice < /dev/tty
...

what happens, what was pointed to you... is that read reads from standard input, and you have globally redirected stdin to the file q0.txt

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

Comments

1

Why don't you simply do:

for line in `cat q0.txt`
do
    echo $line
    python find_entity.py $line
    read -p "Enter your name : " choice
    echo $choice
done

Your code does not ask for any input because you are redirecting the content of q0.txt to the standard input, and the "read" statement gets the choice variable from the same place.

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.