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

发布于 2024-11-27 14:51:01 字数 2345 浏览 1 评论 0原文

如果我需要擦除硬盘或者需要在另一台机器上安装 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

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

我不明白为什么,if...then...else...fi 循环中还有其他读取函数,并且它们按预期工作...

任何想法?

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

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"...

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再见回来 2024-12-04 14:51:01

代码的相关部分仍然不完整,但根据注释,我猜测您的 while 循环看起来像

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

    # read user input
    read -r var

done < file

这样,问题立即显而易见:内部 read 正在从以下位置获取输入与外循环相同的位置,即从 file 重定向的 stdin,而不是用户。对于不依赖于 /dev/tty 的内核级支持的稍微更便携的替代方案,只需在 while 循环中使用 stdin 以外的不同文件描述符即可。

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

请注意,此示例使用 fd 9 作为文件,仅保留 fd 0 (stdin)。请查看 BashFAQ 089 了解更多详细信息。

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 read -r ... ; do 
    # do stuff ...

    # read user input
    read -r var

done < file

From this the problem is immediately apparent: the inner read is 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.

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 089 for more details.

内心荒芜 2024-12-04 14:51:01

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

read secondyn </dev/tty

Try reading from the controlling terminal device:

read secondyn </dev/tty
浅语花开 2024-12-04 14:51:01
read secondyn < /proc/${PPID}/fd/0

这将查看父级的输入,该输入仍然应该是标准输入。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文