Bash:函数重复而不是运行指定的函数

时间:2013-11-02 03:44:14

标签: bash if-statement

我正在用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()和其他一些怪癖都有错误,但我只是在填写扩展指令之前尝试测试这些函数......

1 个答案:

答案 0 :(得分:1)

一些提示:

  • installexityes都是命令或shell内置函数。给你的符号这些名字可能会导致意想不到的结果。我强烈建议将这些函数重命名为不太可能导致这种命名空间冲突的东西。
  • set -x添加到脚本的开头(在!#/bin/bash之后)打开非常有用的调试
  • clear命令将无助于清除任何提示,包括调试set -x的输出 - 您最好将clear命令注释掉 - 至少是为了调试。
  • 注意你的变量范围 - 如果函数A调用函数B,那么函数B将可以访问函数A中设置的变量。

话说回来之后,在调用exit函数之前,您的顶级作用域会将$ proceed设置为“n”。如果exit函数中的$ yes设置为“y”,exit将调用installinstall然后立即检查$proceed是否为“y”,它不会是,因为它在顶级范围内设置为“n”。因此,在这种情况下,安装将始终再次呼叫exit

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