9.10. Processing Command Line Options with getopts
If you are writing scripts that require a number of command line options, positional parameters are not always the most efficient. For example, the Linux lscommand takes a number of command line options and arguments. (An option requires a leading dash; an argument does not. ) Options can be passed to the program in several ways: ls -laFi, ls -i -a -l -F, ls -ia -F, and so forth. If you have a script that requires arguments, positional parameters might be used to process the arguments individually, such as ls -l -i -F . Each dash option would be stored in $1, $2, and $3, respectively. But, what if the user listed all of the options as one dash option, as in ls -liF? Now the -liF would all be ...
Get Linux Shells by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.