Register
It is currently Tue Sep 30, 2014 5:56 pm

Getops with more then one character FLag? How do I?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Jul 11, 2011 5:18 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
Hello all,

I have a pretty insane script with lots of flags:

Code:
while getopts "h:a:u:f:t:e:r:n:s:c:l:j:i:q:w:o:L:B:W:" OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         a)
             accelurl=$OPTARG
             ;;
         u)
             unaccurl=$OPTARG
             ;;
         t)
             times=$OPTARG
             ;;
         f)
             filename=$OPTARG
             ;;
         e)
             emailto=$OPTARG
             ;;
         r)
             reten=$OPTARG
             ;;
         q)
             hq_pop=$OPTARG
             ;;
         n)
             ny_pop=$OPTARG
             ;;
         s)
             sf_pop=$OPTARG
             ;;
         c)
             chi_pop=$OPTARG
             ;;
         l)
             ldn_pop=$OPTARG
             ;;
         j)
             jpn_pop=$OPTARG
             ;;
         w)
             tai_pop=$OPTARG
             ;;
         o)
             la_pop=$OPTARG
             ;;
         L)
             Linux=$OPTARG
             ;;
         B)
             BSD=$OPTARG
             ;;
         W)
             Windows=$OPTARG
             ;;
         ?)
             usage
             exit
             ;;
     esac
done


now I want to be able to add 2 new flags. -F1 & - F2

so for example I could run this:

./myscript -F1 My_Label1 -F2 MY_second_Label

however when I try and just add

F1
F2

Quote:
while getopts "h:a:u:f:t:e:r:n:s:c:l:j:i:q:w:o:L:B:W:L1:L2:" OPTION


Code:
         L1)
             Label1=$OPTARG
             ;;
         L2)
             Label2=$OPTARG
             ;;


This does not work.

Any ideas? Is the problem because it is 2 characters long?



Thanks
~Dynacade


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 5:46 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Yes, getopts(0) isn't able to parse XF86-style long options ( e.g., « -option » ).
You have to write the parsing part yourself. You might be interested in this page.


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 7:53 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
Thanks again for your reply JSZ

Unfortunately that fix would cause a lot of rewrite headache.

So I decided to just add

-1
-2

so now I can do

./myscript -1 this_is_first_label -2 this_is_second_label

but I get a new error. is it ok to use numbers as flags?

my error:
-1: command not found

thanks
~Dynacade


Top
 Profile  
 PostPosted: Tue Jul 12, 2011 12:53 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Yes, you can use a number as an option. Don't know why you're getting this error.


Top
 Profile  
 PostPosted: Tue Jul 12, 2011 3:34 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
dynacade wrote:
my error:
-1: command not found

thanks
~Dynacade

Probably a variable name ($OPTARG or other) as the first thing on a line?


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 11 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:  
cron


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