bash Linux:在案例中调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22635036/
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 10:01:10  来源:igfitidea点击:

Linux: Calling a function within a case

bashfunctionswitch-statement

提问by Seatter

I am trying to set up a menu system where on selection it runs a function. In my case is runs the 'testfunc' function. However is it fails giving the error; testfunc: command not found.

我正在尝试建立一个菜单系统,在其中选择它运行一个功能。在我的情况下是运行'testfunc'函数。但是它没有给出错误;testfunc:找不到命令。

My case statement looks like this;

我的案例陈述看起来像这样;

case "$mainMenuInput" in
   1)testfunc ;;
esac

function testfunc{
    echo "This is a test"
}

Thanks in advance.

提前致谢。

回答by Jan Hudec

Shell scripts are executed statement at a time. The function is only known from the point you define it. You have to move it before the call.

Shell 脚本一次执行一条语句。该函数仅在您定义它时才知道。你必须在通话前移动它。