Linux – Multiple parameters to bash script

bashlinuxshell-scriptunix

I need to check for user provided options in my bash script, but the options won't always be provided while calling the script. For example the possible options can be -dontbuild -donttest -dontupdate in any combination, is there a way I could check for them? Sorry if this question is real basic, I'm new to bash scripting.

Thanks

EDIT:
I tried this chunk of code out, and called the script with the option, -disableVenusBld, but it still prints out "Starting build". Am I doing something wrong? Thanks in advance!

while [ $# -ne 0 ]
do
    arg="$1"
    case "$arg" in
        -disableVenusBld)
            disableVenusBld=true
            ;;
        -disableCopperBld)
            disableCopperBld=true
            ;;
        -disableTest)
            disableTest=true
            ;;
        -disableUpdate)
            disableUpdate=true
            ;;
        *)
            nothing="true"
            ;;
    esac
    shift
done

if [ "$disableVenusBld" != true ]; then
    echo "Starting build"
fi

Best Answer

Dennis had the right idea, i'd like to suggest a few minor mods.

Arguments to shell scripts are accessed by positional parameters $1, $2, $3, etc... and the current count comes in $#. The classical check for this would be:

while [ $# -ne 0 ]
do
    ARG="$1"
    shift # get rid of $1, we saved in ARG already
    case "$ARG" in
    -dontbuild)
        DONTBUILD=1
        ;;
    -somethingwithaparam)
        SAVESOMEPARAM="$1"
        shift
        ;;
# ... continue
done

As Dennis says, if your requirements fit into getopts, you're better off using that.

Related Question