0

I have a script that launches certain nodes based on what arguments you enter

case "$1" in
    start)
        if [ "$2" == "puppet" ]; then
           set_puppet_variables
           check_ES_reqs
           start
        elif [ "$2" == "puppet1" ]; then
           set_puppet1_variables
           check_ES_reqs
           start
        elif [ "$2" == "master" ]; then
           set_master_variables
           check_ES_reqs
           start
        fi
        if [ "$2" == "" ]; then
             set_puppet_variables
             check_ES_reqs
             start
             set_master_variables
             check_ES_reqs
             start
         fi

I want to be able to launch multiple specific nodes for example when I type in command service ES start puppet puppet1 it will then launch only those two nodes.

Is there a way to format the $2 in my logic to accept $3, $4 depending on how many nodes I add? as there will be more. Like making the $2 to a $2* to accept the second argument and any others so I can launch multiple specific nodes.

Please help

Thank you

2
  • 1
    You could loop through the puppet options with the bash shift command. There's a lot of replicated operation in each of those ifs. Commented Jan 14, 2019 at 22:02
  • FYI it is acceptable to have a case inside another case. Commented Jan 14, 2019 at 22:08

2 Answers 2

2

Shift $1 out of the argument list, then loop over the remaining arguments.

case "$1" in
    start)
        shift
        while [ $# -ne 0 ]
        do
            case "$1" in 
                puppet)
                    ... ;;
                puppet1)
                    ... ;;
                master)
                    ... ;;
                "")
                    ... ;;
            esac
            shift
        done ;;
Sign up to request clarification or add additional context in comments.

Comments

2

shift off your command, work on the rest. This is more or less what I'm expecting you require:

cmd="$1"
shift; # get rid of it.
if [ $# -eq 0 ]
then
  # if there are no parameters, use puppet and master
  set -- puppet master
fi

case "$cmd" in
  start)
    for node in "$@"
    do
      set_variables "$node" # slight rename here, make it easier to reuse
      check_ES_reqs
      start
    done
    ;;
  # ...
esac

Comments

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.