如何从shell脚本中的case调用函数?

kas*_*yap 4 bash scripts

大家好,我正在尝试使用 case 语句运行 shell 脚本

opt=$1
case $opt
in
    u) function1 ;;
    g) function2 ;;
    *) exit ;;
esac

function1()
{
    xyz commands
}

funciton2t()
{
    xyz commands
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

function1: command not found
Run Code Online (Sandbox Code Playgroud)

Rav*_*ina 5

当你调用一个函数时,它应该被定义和知道,当你function1像这样调用时:

u) function1 ;;
Run Code Online (Sandbox Code Playgroud)

解释器不知道这个函数在哪里,因为它还没有看到它;所以把你的功能放在case句子上面。

那将解决问题。