jberthia
jberthia

Reputation: 141

using case statement in bash to pass in only arguments

I have the following code in my script:

case "$1" in
    (-h)
            display_help
            exit 0
            ;;
    (start)
            start_services
            ;;
    (stop)
            stop_services
            ;;
    (*)
            display_help
            exit 0
            ;;
esac

I want to be able to pass in an argument when one of these commands is called. For example, I want to start only a users service. I would issue the following command:

./services.sh start users

If I just enter:

./services.sh start

That works as expected, all services are started. However, if I issue the command with a service attached to it, like described above, it still starts all services, not just that service. The code in the start_services() function looks for an argument to start that service.

How do I get it so that it only starts that one service within the case statement?

Upvotes: 1

Views: 685

Answers (1)

PesaThe
PesaThe

Reputation: 7499

If start_services looks for an argument, you have to pass it:

start_services "$2"

Upvotes: 3

Related Questions