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:
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 :
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: