Command Parameters


Table of Contents

commandline switches 
single switch (or in sequential order) 
optional command parameters 
more than one switch in arbitrary order 
Many switch, any order, with parameters 
Meaningful Symbols 
cmd:getopt 
getopt typical usage 
Compatibilities 
Notes 
getopt example: getopt-parse.bash 
optional argument 
Trying history 
unknown -options vs. GNU getopt 
unknown -options vs. GNU getopt 
unknown -options vs. GNU getopt 
getopt, What's the right way to use 
getopt, the right way to use 
getopt, the right way to use 
get the last parameter 
General 
Simple but limited 
cmd:bash positional parameter 
Usage 
Help 
Comments 
How to pass switch/parameter pair 
Symptom 
Solution / Conclusion 

commandline switches 

*Tags*: command line parameters

How to handle command swithes in scripts. Here are the examples from the simplest to the most powerful one.

single switch (or in sequential order) 

[ "x$1" = "x-l" ] && {
  log_url=T
  shift
  }
[ "x$1" = "x-t" ] && {
  file_title=$2
  shift 2
  }

optional command parameters 

[ ${1+T} ] || fhelp
mto=$1
shift
mfm=$1
shift
[ ${1+T} ] && { msg=$1; shift; }

more than one switch in arbitrary order 

while :; do
    case "$1" in
    -s*) fl_sys=T ;;  # system file listing
    -p*) shift; prefix="$1" ;;        # for the beg'ing of filename
    -s*) shift; surfix="$1" ;;        # for end of filename, bfr .tgz
    *)  break ;;
    esac
    shift
done

Many switch, any order, with parameters 

use getopt, but really suggest using the previous method for compatibility reasons.

set -- `getopt "clhstx" "$@"` || fhelp
while :; do
    case "$1" in
    -s) fl_sys=T ;;   # system file listing
    -c) fl_csh=T ;;   # csh/tcsh system file listing
    -p*) shift; prefix="$1" ;;        # for the beg'ing of filename
    -t*) shift; surfix="$1" ;;        # for end of filename, bfr .tgz
    --) shift; break ;;
    esac
    shift
done

and can be called with combined switch such as '-sc..t'.

Meaningful Symbols 

if [ "$1:+T" -a "$1" != "@"  ] ; then
  saved_name=$1
  shift
fi

documented on: 2004.07.10