#!/bin/bash # Example of doing things with optional args in shell scripts using optargs # yes, shell scripts have functions usage() { echo "usage optargs1.sg [-s] [-r] [-f filename] [-d dropname] [-h]" exit 2 } if [[ $# -eq 0 ]] then usage # note that function call to a no arg fun does not have params fi while getopts 'hsrd:f:' c #param that have required additional arg get a : do case $c in # case is equivalent to C switch s) echo "Save" ;; r) echo "Restore" ;; d) echo "Drop $OPTARG" ;; f) echo "File $OPTARG" ;; h) usage ;; esac # end the case done shift $((OPTIND - 1)) # throw away all of the used command line params echo "REST: $*"