Calling function, based on agrument passed.

Dears,

#!/bin/bash
func1()
{
echo "func1"
}

func2()
{
echo "func2"
}

func3()
{
echo "func3"
}

I want to call this function based on argument passed at run time.

like if default:
func1
func2
else
func3

how can i do that? kindly guide

Regards,
sadique

What would "argument passed at run time" look like? And, what does "like if default" mean?

Dear RudiC,

when I should run the script, i want to pass an parameter.
based on the parameter, it should check and run only those function which is pass for that parameter.

---------- Post updated at 06:41 AM ---------- Previous update was at 06:40 AM ----------

like sh test.sh default,
so it should call only func1 and func2
or
sh test.sh xxxxx
then func2 func3 like that.

"Like that" is not too precise a specification. With a vague request like above I'm afraid yo need to be content with fuzzy answers. Like:
Use a case statement to scan through the possible parameter values and execute the functions accordingly.

"Default" usually is the fallback case when nothing is specified; it would be used / executed if NO parameters are given, i.e. $# == 0 . You could test that outside the case construct.

#!/bin/bash

func1()
{
echo "func1"
}

func2()
{
echo "func2"
}


func3()
{
echo "func3"
}


if [[ $# == 0 ]]
then
func1
func2
func3
fi

how do i apply case, kindly help.

---------- Post updated at 07:21 AM ---------- Previous update was at 07:10 AM ----------

Thanks RUdiC for the kind help.

I am able to get what i want now.

#!/bin/bash

func1()
{
echo "func1"
}

func2()
{
echo "func2"
}


func3()
{
echo "func3"
}


if [[ $# == 0 ]]
then
func1
func2
func3
elif [[ -n $1 ]]
then
option=$1
fi
case $option in test)
        func2
        ;;
                check)
        func3
        func1
        ;;
esac

OK, brilliant, that's a start (talking of post#5)! You did not yet specify how those parameter(s) should look like ... one parameter, several characters/numbers?

Did you read bash 's man page on the case ... esac (a professional approach, btw)? It would lead you to something like

case $1 in 
        12)     func1
                func2
                ;;
        13)     func1
                func3
                ;;
        23)     func2
                func3
                ;;
        *)      echo failure / usage
                ;;
esac

EDIT: Cross- Posted, unfortunately.