3

I have a script in which accepts the following arguments: ./script [-dph] [-peh] The first argument tells the script if I need to execute it with production scripts or with development scripts. The second argument tells the script if the output of the report generated should be sent as an email or should be redirected to a html page.

When I use getopts, I get an option to do with one option.

while getopts "dph" opt_name
do
    case "$opt_name" in
        h)
            helpText
            exit 1
            ;;
        d)
            #Use Dev env
            ;;
        p)
            #Use Prod env
            ;;
        *)
            echo "Wrong command line argument. Exiting...";
            exit 1
            ;;
    esac
done

How do I do it with two options in which the second argument means a different action based on the first argument?

P.S.: Please note that I know that having options for dev and prod env is not the best solution around, but my app is in a proof of concept stage and hence, I am using whatever I know to build this.

2 Answers 2

3

This is maybe not the most elegant solution and I'd be happy to find out about better approaches, but it works and what it does is clear.

In the getopts part you parse the command line options and assign them to variables. In the second part you control the cli option dependencies and you can directly execute the actions in the control structure.

#!/bin/bash

while getopts "DPehp" opt_name 
do
    case "$opt_name" in
        h)
            helpText
            exit 1
            ;;
        D)  
            #Use Dev env
            dev=true
            ;;
        P)  
            #Use Prod env
            prod=true
            ;;
        p)  
            p=true
            ;;
        e)  
            e=true
            ;;
        *)
            echo "Wrong command line argument. Exiting...";
            exit 1
            ;;
    esac 
done

if [[ $dev == true && $prod == true ]] || [[ $dev != true && $prod != true ]]
then
    echo "You must choose between development (-D) or production (-P) scripts"
    exit 
fi

if [[ $dev == true ]] 
then
    if [[ $p == true ]] 
    then
        echo "Your 'p' dev action goes here"
    fi
    if [[ $e == true ]] 
    then
        echo "Your 'e' dev action goes here"
    fi 
elif [[ $prod == true ]] 
then
    if [[ $p == true ]] 
    then 
        echo "Your 'p' prod action goes here"
    fi
    # and so on fi
fi
Sign up to request clarification or add additional context in comments.

Comments

0

Simple solution: use different letters for development/production flags (e.g. capital D and P).

Complex solution: under p), check whether the environment has been decided. If so, use the second meaning. If not, set environment to production.

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.