Skip to content
Advertisement

Bash script does not wait for user to enter response

I am trying to read a text file (line by line) and take user response in a while loop. However, the script does not wait to take input. Instead, it just prints all the text to screen.

while IFS= read -r line || [ -n "$line" ]; do
    printf '%sn' "Store $line y or n: "
    read input
    if [ $input == "y" ]
    then
        echo $line >> saved_domains.txt
    fi
done < "urls.txt"

The script only prints alternate text lines from the file (Please refer to the image below).

Output

Advertisement

Answer

This worked for me:

exec 3<&0
while IFS= read -r line || [ -n "$line" ]; do
    printf '%sn' "Store $line y or n: "
    read -r -u3 input
    if [ $input == "y" ]
    then
        echo $line >> saved_domains.txt
    fi
done < "urls.txt"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement