ijava学习网> 知识分享> Linux> Getopt and getopts

Getopt and getopts

时间: 2019-03-14 16:02:08 标签#shell脚本相关
Getopt and getoptshttp://aplawrence.com/Unix/getopts.html

Both "getopt" and getopts are tools to use for processing and validating shell script arguments. They are similar, but not identical. More confusingly, functionality may vary from place to place, so you need to read the man pages carefully if your usage is more than casual.

Properly handling command line arguments is difficult if you want the usage to be flexible. It's easy to write a script that demands arguments in a specific order; much harder to allow any order at all. It's also hard to allow bunched together arguments or spaced out to be equivalent:


Code anonymous
Great! thank's a lot!



Tue Jun 7 15:17:15 2005: Subject:
anonymous
Of course, getopts doesn't handle long option names, while getopt does.



Tue Jun 7 18:03:59 2005: Subject:
BigDumbDinosaur
Of course, getopts doesn't handle long option names, while getopt does.

So don't use long option names. Besides, typing long option names gets annoying in a big hurry. If you need to specify more than 52 options (that 26 LC and 26 UC for those who are not familiar with UNIX shell case sensitivity), how about using a config file?



Mon Jun 20 23:42:37 2005: Subject: Awesome!
dannyman
Okay, but after I loop through getopts, how do I reset the $@ stuff in bash so that I can read in subsequent arguments? For a thing like:



./foo.sh -a -b -c file.conf arg1 arg2 arg3 ...

Wed Jun 22 20:02:16 2005: Subject:
TonyLawrence
By doing OPTIND=1




Fri Aug 5 22:51:11 2005: Subject:
anonymous
or maybe :

shift $((OPTIND-1))

Fri Sep 23 20:34:42 2005: Subject: getopt is bad
anonymous
(your comments go here)


While I understand it's never going away, getopt() is a really bad, bad, bad piece of code. This is the part of it in C I hate:
extern int optind;
extern char *optarg;

I detest functions like this that use hard coded externs that make programmers not older than dirt scratch their head and say , what the heck?






Fri Sep 23 22:36:23 2005: Subject:
BigDumbDinosaur
I detest functions like this that use hard coded externs that make programmers not older than dirt scratch their head and say, "what the heck?"

Whaddya mean "older than dirt?" I completely resemble that remark.

Seriously, library functions like getopt are part of the glue that holds a coherent UNIX API together. I have a big problem with some of the programming youngsters who, rather than understand and work with standard library functions that we old dinosaurs have used for decades, want to come up with something different just because they don't understand the existing function or the associated variable names (optind makes perfect sense to me -- it's option index). It's that sort of narrow thinking that has resulted in some of the language abortions we have today, such as Java and C++.

BTW, just how would you implement a library function like getopt and provide identical functionality, without any use of "hard coded externs"?




Fri Oct 28 17:36:01 2005: Subject: Get the rest from getopts
anonymous
The comments above asked about getting the rest of the args from getopts and suggested setting OPTIND. I hacked out something, and I think it's a better way, something like this:



max=0

while getopts "eprvh" flag

do

case $flag in

... (omitted)

esac



if [ $OPTIND -gt $max ]

then

#echo "setting max to $OPTIND"

max=$OPTIND

fi

done



shift $max



# now $@ just has what's left

# note this only works if you program is called with options first like

# gg -evp arg1 arg2

# and not

# gg -ev arg1 -ph arg2

#

# Hope this helps - Ken

Thu Nov 17 14:21:40 2005: Subject:
anonymous
In your case statement, just useshift $((OPTIND-1)); OPTIND=1after you've processed a certain option (and it's argument).



bartX

Thu Dec 14 20:12:02 2006: Subject:
Dave
Found this very useful, thanks!



Thu Dec 14 23:45:42 2006: Subject:
BigDumbDinosaur
Naturally, all of this getopts maneuvering is thoroughly documented in a 1000 places (including here), and buried somewhere in that documentation (What!!!??? I have to actually read this crap???) is the little part about usingshift $((OPTIND-1)); OPTIND=1to reset the option index to a new "ground zero" setting. Obviously, the anonymous person who pointed this out *has* read the documentation.



Wed Jan 23 16:59:10 2008: Subject:
anonymous


Thanks for this article -- it's a shame that the bash info file is so example free. I could have never figured out what was meant from that alone.

Fri Feb 8 06:56:52 2008: Subject:
anonymous


Great article, very useful! Thank you a lot!

版权说明| 关于ijava| 合作伙伴| 联系我们| 网站地图| 招贤纳士

Copyright © 2017 www.ijava.com All Rights Reserved 版权所有•ijava学习网 京ICP备14061482号-18         官方QQ:3325669927

ijava学习网提供免费java教程和大量java面试题库,给高级会员提供免费的java培训,同时提共一些java开发项目和java下载,java工程师,java菜鸟们快来哦。记住我们的网站:www.ijava.com