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

时间:2011-08-02 11:47:10

标签: bash user-input

如果我需要擦除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

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

我无法弄清楚为什么,if...then...else...fi循环中还有其他读取函数,它们按预期工作...

有什么想法吗?

3 个答案:

答案 0 :(得分:12)

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

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

    # read user input
    read -r var

done < file

由此问题显而易见:内部read从外部循环获取其输入,即从file重定向的stdin,而不是用户。对于不依赖于/dev/tty的内核级支持的稍微更便携的替代方法,只需使用stdin以外的其他文件描述符作为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

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

答案 1 :(得分:7)

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

read secondyn </dev/tty

答案 2 :(得分:1)

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

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