#!/bin/bash # A custom handler for optional args # Largely reproduces optags1.sh, but does more usage() { echo "usage optargs2.sh [-s | --save] [-r | --restore [filename]] [--file | -f filename] [--drop | -d dropname] [-t | --triple single double] [--help | -h]" exit 2 } while [[ -n "$1" ]] # quit if there are no more command line params do case "$1" in -h|--help) usage ;; -s|--save) echo "Save" shift ;; -r|--restore) regexInt='^[0-9]+$' if [[ $2 =~ $regexInt ]]; then echo "restore $2"; shift 2 else echo "restore" shift fi ;; -d|--drop) echo "drop $2" shift 2 ;; -f|--file) echo "file $2" shift 2 ;; -t|--triple) echo "triple $2 $3" shift 3 ;; *) # On any unknown arg exit the while loop so those args # can be processed elsewhere break ;; esac done echo "AAA $*"