bash 脚本中的“读取”命令被跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6911520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:29:54  来源:igfitidea点击:

"Read" command in bash script is being skipped

bashuser-input

提问by jw013

I'm creating a script to update my linux distribution if I need to wipe the HD or I need to install Linux on another machine. So this script basically install all the programs I usually need. At the beginning a have a "read" command that asks if I want to install all the packages automatically or not. If I choose not, for each program not found it should ask me I want it to be installed and I use this code

如果我需要擦除 HD 或者我需要在另一台机器上安装 Linux,我正在创建一个脚本来更新我的 linux 发行版。所以这个脚本基本上安装了我通常需要的所有程序。一开始有一个“读取”命令,询问我是否要自动安装所有软件包。如果我选择不,对于未找到的每个程序,它应该询问我是否要安装它并使用此代码

if [[ $installall == "yes" ]]; then
    echo " Installing $sciprog..."
    sudo apt-get install -y $sciprog >/dev/null
    {
        scitest=`dpkg -s $sciprog | grep Status`
    } 2>${HOME}/musthave.errorlog
    if [[ $scitest != "Status: install ok installed" ]]; then
        echo " I've encountered problems installing $sciprog that I can't resolve. "
        echo " Consider installing $sciprog manually. "
        {
            echo "=========="
            echo " $sciprog"
        } >>${HOME}/musthave.notinstalled
    else
        echo " $sciprog installed correctly!"
        {
            echo "=========="
            echo " $sciprog"
        } >>${HOME}/musthave.installed
    fi
else
    echo " Seems like $sciprog is not installed... Do you want to download it?"
    echo " Type 'y' for yes."

    read secondyn ### THIS IS THE GUILTY COMMAND ###

    if [[ $secondyn == "y" ]]; then
        echo " Installing $sciprog ..."
        sudo apt-get install -y $sciprog >/dev/null
        {
            checkinstall=`dpkg -s $sciprog | grep Status`
        } 2>>${HOME}/musthave.errorlog
        if [[ $checkinstall != "Status: install ok installed" ]]; then
            echo " I've encountered problems installing $sciprog that I can't resolve. "
            echo " Consider installing $sciprog manually. "
            {
                echo "=========="
                echo " $sciprog"
            } >>${HOME}/musthave.notinstalled
        else
            echo " $sciprog installed correctly!"
            {
                echo "=========="
                echo " $sciprog"
            } >>${HOME}/musthave.installed
        fi
    else
        echo " Skipping $sciprog ..."
        {
            echo "=========="
            echo " $sciprog"
        } >>${HOME}/musthave.notinstalled
    fi
### some more code which works as expected. All the code above is inside a 
### while...do...done loop which reads line by line the file at the end
done <${HOME}/file.list

But if I run the script, it skips the "read" command in the else clause and assumes it to be "n"...

但是如果我运行脚本,它会跳过 else 子句中的“读取”命令并假定它是“n”...

I can't figure out why, there are other read function also inside if...then...else...filoops and they work as expected...

我不知道为什么,if...then...else...fi循环内还有其他读取函数,它们按预期工作......

Any ideas?

有任何想法吗?

回答by jw013

The relevant portions of the code are still not complete but based on the comments I'm going to guess that your while loop looks like

代码的相关部分仍然不完整,但根据评论我猜你的 while 循环看起来像

while read -r ... ; do 
    # do stuff ...

    # read user input
    read -r var

done < file

From this the problem is immediately apparent: the inner readis getting its input from the same place as the outer loop, namely stdin which has been redirected from file, and not the user. For a slightly more portable alternative that does not depend on kernel-level support for /dev/tty, just use a different file descriptor other than stdin for the while loop.

从此问题就很明显了:内部read是从与外部循环相同的地方获取输入的,即从 重定向的标准输入file,而不是用户。对于不依赖于内核级支持的稍微更便携的替代方案/dev/tty,只需为 while 循环使用标准输入以外的不同文件描述符。

while read -r ... <&9; do
    # loop stuff

    # stdin still attached to the terminal untouched, 
    # so this reads from the terminal as expected
    read -r var 

done 9< file

Notice that this example uses fd 9 for the file, leaving fd 0 (stdin) alone. Take a look at the BashFAQ 089for more details.

请注意,此示例对文件使用 fd 9,单独保留 fd 0 (stdin)。有关更多详细信息,请查看 BashFAQ 089

回答by jon

Try reading from the controlling terminal device:

尝试从控制终端设备读取:

read secondyn </dev/tty

回答by brightlancer

read secondyn < /proc/${PPID}/fd/0

That will look to the parent's input, which should still be stdin.

这将查看父项的输入,该输入仍应为 stdin。