Bash scripting

Lynn David Newton plug-discuss@lists.plug.phoenix.az.us
Mon, 29 Jul 2002 13:59:09 -0700


  Bryce> It moves the second parameter passed to the
  Bryce> first's variable if there was a match. I don't
  Bryce> want to be tied down to a certain set/order of
  Bryce> the parameters. I'm simply trying to
  Bryce> move/rename $2 to $1 which is very difficult
  Bryce> because a variable name can't be number. Hope
  Bryce> that helps.

Is there a reason you're tied to the positional
parameters? How about just reassigning them to ordinary
variables names?

I'm guessing perhaps you are trying to deal with
parsing options? If so, how about using getopts, which
is designed exactly for this purpose?

Some sample code taken from a working program (written
in ksh, not bash, but is should run pretty much the
same):

# parse options
while getopts c:fhl:p:s:t: opt
do
    case $opt in
	c)
	    CACHEDIR=$OPTARG
	    [[ -d $CACHEDIR ]] || {
		print -u2 "No cache directory: $CACHEDIR"
		exit 1; }
	;;
	f)
	    fflag=1
	;;
	h)
	    helpmsg $(basename $0)
	    exit 0
	;;
	l)
	    SYSLOGIN=$OPTARG
	;;
	p)
	    PROG=$OPTARG
	;;
	s)
	    SYS=$OPTARG
	;;
	t)
	    TMPDIR=$OPTARG
	    [[ -d $TMPDIR ]] || {
		print -u2 "No temporary directory: $TMPDIR"
		exit 1; }
	;;
	*)  # fallthrough for bad option
	    print -u2 "$USAGE"
	    exit 1
	;;
    esac
done

# shift out options and arguments
shift $((OPTIND - 1))

-- 
Lynn David Newton
Phoenix, AZ