Option Parsing in Shell Scripts
In the previous example, you manually handled the options passed to your script. In this second example, you will explore a second method, using the getopts command. The syntax of the getopts command is as follows:
getopts option-string var
Here option-string is a string consisting of all the single character options getopts should consider and var is the name of the variable that the option should be set to. Usually var is a variable named OPTION.
The process by which getopts parses the options given on the command line is as follows:
getopts examines all the command-line arguments, looking for arguments starting with the - character.
When an argument starting with the - character is found, it compares ...
Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition 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.