Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopts



> #!/bin/sh
> #
> #       Handles Arguments
> #
> 
> while getopts xy: option

The first problem is with you optstring.  If both the 'x' and 'y' 
options require an argument (which is what your help text implies), then 
your optstring should be 'x:y:'.

> echo $OPTARG

What's this echo statement doing in the expression portion of your while 
statement?  I think you meant for this to come after the 'do' on the 
next line.  The way its current positioned, your while statement looks 
like this:

   while true; do

Which would loop forever, except that getopts sets option to '?' if 
there are no more arguments, so instead it bails out in your case statement.

> do
>         case $option in
>                 x) xdim=$OPTARG;;
>                 y) ydim=$OPTARG;;
>                 \?) echo "usage: ds9sh [-x columns] [-y columns] file..."
>                 exit 2;;
>         esac
> done
> shift `expr $OPTIND - 1`

This ought to work for you:

   while getopts x:y: option; do
     case $option in
       x) xdim=$OPTARG;;
       y) ydim=$OPTARG;;
       \?) echo "usage: ..."
           exit 2;;
     esac
   done
   shift `expr $OPTIND - 1`

Note that I've replaced some EOLs with semicolons, which is just my 
formatting preference.  The following two statements are equivalent:

   while true; do
     echo hello
   done

And:

   while true
   do
     echo hello
   done

-- Lars





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org