How do I handle switches in a shell script?

44,895

Solution 1

Use getopts.

It is fairly portable as it is in the POSIX spec. Unfortunately it doesn't support long options.

See also this getopts tutorial courtesy of the bash-hackers wiki and this question from stackoverflow.

If you only need short options, typical usage pattern for getopts (using non-silent error reporting) is:

# process arguments "$1", "$2", ... (i.e. "$@")
while getopts "ab:" opt; do
    case $opt in
    a) aflag=true ;; # Handle -a
    b) barg=$OPTARG ;; # Handle -b argument
    \?) ;; # Handle error: unknown option or missing required argument.
    esac
done

Solution 2

I assume you're using bash or similar. An example:

all=false
long=false

while getopts ":hal" option; do
  case $option in
    h) echo "usage: $0 [-h] [-a] [-l] file ..."; exit ;;
    a) all=true ;;
    l) long=true ;;
    ?) echo "error: option -$OPTARG is not implemented"; exit ;;
  esac
done

# remove the options from the positional parameters
shift $(( OPTIND - 1 ))

ls_opts=()
$all && ls_opts+=( -a )
$long && ls_opts+=( -l )

# now, do it
ls "${ls_opts[@]}" "$@"

Solution 3

You have to write a cycle to parse the parameters. Indeed you can use getopts command to do it easily.

This is a simple example from getopts manual page:

aflag=
bflag=
while getopts ab: name
do
    case $name in
    a)    aflag=1;;
    b)    bflag=1
          bval="$OPTARG";;
    ?)    printf "Usage: %s: [-a] [-b value] args\n" $0
          exit 2;;
    esac
done
if [ ! -z "$aflag" ]; then
    printf "Option -a specified\n"
fi
if [ ! -z "$bflag" ]; then
    printf 'Option -b "%s" specified\n' "$bval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

Solution 4

I wrote a script recently for work that was versatile and allowed for multiple kinds of switches in any order. I can't disclose the full script for obvious legal reasons (not to mention I don't have it with me at the moment), but here's the meat of it.. you can put it in a subroutine and call it at the end of your script:

options () {

    if [ -n "$1" ]; then # test if any arguments passed - $1 will always exist
        while (( "$#" )); do  # process ALL arguments
            if [ "$1" = ^-t$ ]; then # -t short for "test"
                # do something here THEN shift the argument
                # shift removes it from $@ and reduces $# by
                # one if you supply no argument
                shift

            # we can also process multiple arguments at once
            elif [[ "$1" =~ ^--test=[:alnum:]{1,8}$ ]] && [[ "$2" =~ ^-t2$ ]] && [[ -n "$3" ]]; then # check for 3 arguments
                # do something more then remove them ALL from the arg list    
                shift 3
            else
                echo "No matching arguments!"
                echo "Usage: [script options list here]"
            fi
        done
    else
        echo "Usage: [script options list here]"
        exit 0
    fi
}

options "$@" # run options and loop through/process ALL arguments

I do recommend limiting your bash script to less than 400 lines/15k characters; my aforementioned script grew past this size and became greatly difficult to work on. I started rewriting it in Perl, which is much better suited for the task. Keep that in mind as you work on your scripts in bash. Bash is great for small scripts and oneliners, but anything more complex and you're better off writing it in Perl.

Note, I haven't test the above, so it probably doesn't work, but you get the general idea from it.

Share:
44,895

Related videos on Youtube

user394
Author by

user394

Updated on September 18, 2022

Comments

  • user394
    user394 over 1 year

    Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments thereafter?

  • clerksx
    clerksx over 12 years
    It should be mentioned that getopt should always be verified as GNU getopt before you use it, but you shouldn't use it anyway as getopts is more portable (and generally nicer) anyway. If you do need to call it for some reason, call it in a GNU-specific way, and ensure that GETOPT_COMPATIBLE is not in the environment.
  • clerksx
    clerksx over 12 years
    The way you call options at the end is not correct, it will return -bash: syntax error near unexpected token $@. Call it as options "$@".
  • laebshade
    laebshade over 12 years
    Yup, I mixed up Perl and Bash. Corrected.
  • manatwork
    manatwork over 12 years
    That while condition should not be (($#)) instead?
  • laebshade
    laebshade over 12 years
    Why would you need two sets of parentheses for $#? Edit: you're right. Fixed it to while (( "$#" ))
  • James Sneeringer
    James Sneeringer over 12 years
    +1 for using += with an array. Didn't know you could do that. Nice!
  • user394
    user394 over 4 years
    What does the colon in while getopts "ab:" opt do?
  • jw013
    jw013 over 4 years
    @user394 The : after an option letter signifies it requires an argument. A : as the first character means to suppress error messages.