0

I need to modify one of my scripts. Until now it has two obligatory parameters which point out to version of update and database where this update is going to be applied

./script.sh version db_name

Now I want to add two new optional parameters, in fact I should call it switches. These switches extend it to : 1. before install stop (or not) my web server 2. install also some new files to filesystem Both of the returns bool value. All details are inside the script. So i expect something like:

./script.sh version db_name -stopweb  -copyfiles

I have figured out getopts is suitable command. The problem is how to "join" parameters (obligatory) and switches (optional) together. I really can't get it :( Could you give me some hints please.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
user3863616
  • 185
  • 1
  • 9
  • 24
  • 1
    you can take a look here: http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – Chris Maes Nov 17 '15 at 15:50
  • right , it is a solution. But my script has been used by several people (and probably called by a few other scripts) so it would be good to keep first two parameters unchanged. So important for me is to keep first two parameter as they are and add optionally two new switches. – user3863616 Nov 17 '15 at 15:59
  • then look at my answer which allows you to do that... – Chris Maes Nov 17 '15 at 16:03
  • finally I have found it ! thanks a lot :) – user3863616 Nov 19 '15 at 09:24
  • If my answer helped you, you might upvote it. If my answer solved your question, then accept it as an answer. If not, consider answering your own question with the solution. – Chris Maes Nov 19 '15 at 13:28

1 Answers1

5

You should consider using getopts. Here is a sample of what I use for option parsing in my bash scripts:

TEMP=$(getopt -o dp:v --long dev,publish:,verbose -- "$@")

# Note the quotes around '$TEMP': they are essential!
eval set -- "$TEMP"

#default values
DEV=0
VERBOSE=
while true; do
    case "$1" in
        -d | --dev ) DEV=1; shift ;;
        -p | --publish ) PUBLISH="$2" ; shift 2;;
        -v | --verbose ) VERBOSE="-v" ; shift ;;
        -- ) if [ -n "$2" ]
            then
                ARGUMENT1=$2
                if [ -n "$3" ]
                then
                    ARGUMENT2=$3
                    if [ -n "$4" ]
                    then
                        shift 3
                        echo "Unexpected options: \"$@\" . exiting."
                        exit 1;
                    fi
                fi
            fi
            shift 2; break;;
        * ) break ;;
    esac
done

# you can add some checks here that ARGUMENT1 and ARGUMENT2 were effectively set, if they are mandatory

some features:

  • use short and long options
  • some options need an argument
  • use some optional arguments (one in this case), that doesn't start with -

NOTE : in order for getopts to be able to distinguish -dv being two short options and --dev being a long option; all your long options should start with two hyphens.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • This is using `getopt(1)` and not `getopts` (they are different things) and while `getopt(1)` supports long options many older versions of `getopt` are broken with options that contain spaces and/or are blank. – Etan Reisner Nov 17 '15 at 16:14
  • @user3863616 : if my answer helped you, please consider accepting it. – Chris Maes Jan 19 '16 at 15:05