Looking at Real-world Examples
Now that I’ve demonstrated the theory of using getopts
, it’s time to get down to business with a couple of real-world examples. Enjoy!
The Modified Coingecko Script
In Chapter 10, Understanding Functions, I showed you a pair of cool scripts that I created for my own use. To review, these scripts use the Coingecko API to automatically fetch information about cryptocurrencies.
The coingecko.sh
script uses an if. .elif. .else
construct to allow me to choose the function that I want to perform. In the coingecko-case.sh
script, I use a case. .esac
construct to achieve the same thing. Now, I present the third incarnation of the script, which uses getopts
.
The coingecko-getopts.sh
script is too large to reproduce here, so you’ll need to grab it from GitHub. I do need to point out a few things about it, so I’ll just show the relevant snippets here.
At the beginning of the script, I added the gecko_usage()
function...