简体   繁体   English

Bash 脚本不等待用户输入响应

[英]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.我正在尝试读取文本文件(逐行)并在 while 循环中获取用户响应。 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 '%s\n' "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输出

This worked for me:这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM