0

How can I get the user input like below which I would like to use inside my shell script to build a case condition?

./iwant2reload-worker.sh --target=cluster --cluster-name=abc
./iwant2reload-worker.sh --target=workernode --workernode-name=xyz

for example : if user issue the first command it needs to run program a and if user issue the second command it needs to run program b.

0

1 Answer 1

1
#!/bin/bash

while [ "$1" != "" ]; do
    PARAM=`echo $1 | awk -F= '{print $1}'`
    VALUE=`echo $1 | awk -F= '{print $2}'`
    case $PARAM in
        --target)
            TARGET=$VALUE
            ;;

        --cluster-name)
            CLUSTER_NAME=$VALUE
            ;;

        --workernode-name)
            WORKNODE_NAME=$VALUE
            ;;

        *)
            echo "ERROR: unknown parameter \"$PARAM\""
            exit 1
            ;;
    esac
    shift
done

#==========================

 if [ ${TARGET} = "cluster" ]
    then
     echo "Run programm A for ${TARGET} & ${CLUSTER_NAME}"
 fi

 if [ ${TARGET} = "workernode" ]
    then
     echo "Run programm B for ${TARGET} & ${WORKNODE_NAME}"
 fi
Sign up to request clarification or add additional context in comments.

2 Comments

The community encourages adding explanations alongisde code, rather than purely code-based answers (see here).
I'd recommend using getopt(1) instead of rolling your own. Read the getopt example script for bash.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.