1

我正在用 Python 编写一个应用程序,所以我正在为 Bash 编写一个安装文件。我遇到了一个问题,exit() 函数重复自身,而不是从 if 语句中调用 install 函数。这是代码...

#! /bin/bash

function install {
if [ $proceed == "y" ];
    then
        echo " "
        echo "Thank you for installing the ACS Troubleshooter!"
        echo " "
        echo "The next line is going to ask for your password to initialize a download"
        echo "sequence from the standard Ubuntu repositories"
        echo " "
            #sudo apt-get install testing
            mkdir ~/Desktop/ACSapplicationFolder
            sudo cp -r test ~/Desktop/ACSapplicationFolder
            sudo chown -R ~/Desktop/ACSapplicationFolder
        echo " "
        echo " "
        echo "The ACS Troubleshooter has been successfully installed."
        read -p "Press [ENTER] key to open the ACS Troubleshooter > "

            python gui.py &
elif [ $proceed == "n" ];
    then
        exit
fi
}

function bad_input {
echo "Please enter 'y' to continue the installation or 'n' to abort..."
    read -p "> " proceed
        if [ $proceed == "y" ];
            then
                install
        elif [ $proceed == "n" ];
            then
                exit
        else
            bad_input
        fi
    }


function exit {
        echo "The installation will exit."
        echo "   Please press [ENTER] to exit the installation or"
        echo "   press 'y' to reattempt installation."
        read -p "> " yes
            if [ "$yes" == "y" ];
                then
                    clear
                    install
            #else
                #exit 1
            fi
}



clear
echo " "
echo "                          *************************"
echo " "
echo "                       INSTALLATION: ACS TROUBLESHOOTER"
echo " "
echo "    The installer is going to install Python, the language this application"
echo "    is written in. Most computers don't have this installed by default so "
echo "    we need to do this before running the Troubleshooter. It's going to ask "
echo "    you to input your password one time to allow permission to install files "
echo "    to sensitive directories."
echo "                          *************************"
echo " "
echo " "

echo "Should we continue with the installation? (type 'y' or 'n' then press enter) "
#echo "> "
read -p "> " proceed

if [ $proceed == "y" ];
    then
        install
    elif [ $proceed == "n" ];
        then
            exit
    else
        bad_input

fi

唯一给我带来麻烦的函数是 exit() - 其他两个完全按预期工作......

测试脚本时,在初始提示符处给出“n”会运行 exit(),但在 exit() 提示符处给出“y”应该会重新运行 install(),但它会重新发出 exit() 提示符......感到沮丧......谁能回答为什么这样做?

*注意:我刚刚开始这样做,所以我知道 install() 和其他一些怪癖中存在错误,但我只是在填写扩展说明之前尝试测试这些功能......

4

1 回答 1

1

一些提示:

  • install,exit并且yes都是命令或 shell 内置命令。为您的符号提供这些名称可能会导致意外结果。我强烈建议将这些函数重命名为不太可能导致此类命名空间冲突的名称。
  • 添加set -x到脚本的开头(之后!#/bin/bash)会打开非常有用的调试
  • clear命令将无助地清除任何提示,包括调试输出set -x- 您最好将clear命令注释掉 - 至少用于调试。
  • 注意您的变量范围 - 如果函数 A 调用函数 B,则函数 B 将有权访问函数 A 中设置的变量。

话虽如此,您的顶级范围将在调用您的exit函数之前将 $proceed 设置为“n”。exit如果在函数中将$yes 设置为“y” ,exit将调用install. install然后立即检查是否$proceed是“y”,它不会是,因为它在顶级范围中设置为“n”。exit所以在这种情况下install 总是会再次调用。

我认为 in 中的if语句install是完全没有必要的,因为已经对用户进行了轮询,并且install调用了之前在每个实例中检查的结果。

于 2013-11-02T04:29:10.723 回答