0

I am trying to do something like:

fn()
{
    "reusedPattern")
        #do stuff
        ;;
}

and call it within a select case

select stuff in ${something[@]}
do
    case $stuff in
       "pattern1")
            #do stuff
            ;;
     ...
       fn
     ...
       *) echo invalid input ;;
done

When executing script, bash doesn't like the function (gives me error regarding the ")" and the ";;").

What would work?

1 Answer 1

1

A function can only have complete commands, not a fragment of a case statement.

What you can do is to define the #do stuff part in a function, and call it from the case command:

reusedPatternCommands()
{
    #do stuff
    echo "reusedPattern"
}

select stuff in ${something[@]}
do
    case $stuff in
       "pattern1")
            #do stuff
            ;;
     ...
       "reusedPattern")
            reusedPatternCommands
     ...
       *) echo invalid input ;;
done
Sign up to request clarification or add additional context in comments.

2 Comments

Is there another type of package that can do what I'm wanting? I'll accept that functions can't do exactly what i'm wanting, but anything else?
You can create a parser that process your script searching for that kind of function, and output an executable shell script with that code inlined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.