Ubuntu – How to call a function from case in shell script

bashscripts

Hi all I am trying to run a shell script with case statement

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

function1()
{
    xyz commands
}

funciton2t()
{
    xyz commands
}

I am getting the following error:

function1: command not found

Best Answer

When you are calling a function, it should be defined and known, when you are calling the function1 at this like:

u) function1 ;;

Interpreter has no idea where this function is, because it has not seen it yet; so put your functions above the case sentence.

That would fix the issue.

Related Question