Getopt and getopts

Both “getopt” and getopts are tools to use for processing andvalidating shell script arguments. They are similar, but notidentical. More confusingly, functionality may vary from place toplace, so you need to read the man pages carefully if your usage ismore than casual.

Properly handling command line arguments is difficult if youwant the usage to be flexible. It’s easy to write a script thatdemands arguments in a specific order; much harder to allow anyorder at all. It’s also hard to allow bunched together arguments orspaced out to be equivalent:

foo -a -b -cfoo -abcfoo -a -c +bfoo -ac +b

These are the problems that getopt(s) are designed to handle.They ease the job considerably, but introduce their own littlequirks that your script will need to deal with.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s