Register
It is currently Tue Sep 30, 2014 3:47 am

Taking optional arguments


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Jul 03, 2008 11:48 pm   

Joined: Thu Jul 03, 2008 11:44 pm
Posts: 1
I've searched the forums a bit, even tried Google. I can't seem to find an answer to how to use optional arguments or parameters.

Aside from the obvious ways like "$4 == '-t'", what is the better way to do this?


Top
 Profile  
 PostPosted: Fri Jul 04, 2008 3:24 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
i use getopts, it's in the bash man page. Here is a simple, imperfect, real world example...

Code:
# parse options
while getopts "hnvr:d:" options
do
  case "$options" in
    v) ((DEBUG++))
       svnopts=
       ;;
    r) if echo "$OPTARG" | egrep -q '[[:digit:]]+'
       then
         revision=$OPTARG
       elif [ "$OPTARG" != "HEAD" ]
       then
         echo "Revision must be a number or 'HEAD', which is the default." >&2
         exit 2
       fi
       ;;
    d) case "$OPTARG" in
         local) prodorstage=local ;;
          prod) prodorstage=prod  ;;
             *) echo "Error: argument to -d must be prod or local"
                myusage;
                ;;
       esac
       ;;
    n) publish=no ;;
    h) myusage;;
    *) myusage;;
  esac
done


Top
 Profile  
 PostPosted: Sat Jul 05, 2008 8:55 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i actually wrote a quick summary for positional parameters (arguments)

you can see it here

hope that helps


Top
 Profile  
 PostPosted: Mon Jul 07, 2008 10:34 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I usually don't bother with getopts, I just do a while $# > 0; case $1 in; foo) bar;; sort of thing.


Top
 Profile  
 PostPosted: Tue Jul 08, 2008 4:10 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Daenyth wrote:
I usually don't bother with getopts, I just do a while $# > 0; case $1 in; foo) bar;; sort of thing.


Which is fine. I like that getopts does some of the validation work for you and standardises some error messages so the code is slightly shorter. But only slightly lol :)


Top
 Profile  
 PostPosted: Wed Aug 27, 2008 9:28 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Okay, I know this is a necro post, but I haven't been here in a while, and I just wanted to mention that

I <3 GETOPTS

That's all.
Thanks!
-Jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP