Scripting is awesome. You can write a script to do almost anything from downloading files and cleaning up HTML to performing system or package maintenance. I have several scripts that automate tasks on my machine, including one for new system setups which installs HomeBrew and a dozen or so commonly used packages, as well as setting preferred OS X defaults. Just run the script and go for coffee.
But sometimes a script needs more smarts. Slicing up your commands into related functions and adding the ability to use command line arguments makes working with scripts even easier, endowing you with the power to cherry pick which commands to execute at runtime.
Here’s how to add functions and command line arguments to your Bash script.
Create a file to hold your variables (optional)
I use a half dozen or so Bash scripts to automate several aspects of my local machine. One thing these scripts have in common are variables, such as custom log filenames, date formatting, etc. Instead of duplicating these variables these across scripts, I put them into a common include file.
Create a file called “my-variables.sh”, add in your variables, and copy it to a convenient location such as Dropbox. Then include the variables in your script file using the “source” command like so:
Slice up your commands into functions
So, far this script doesn’t do anything except import your variables. So, let’s add in some commands that do stuff.
Okay, so this is just an example that dumps a list of outdated HomeBrew files to a textfile. Note that the variable $PACKAGESOUTDATEDLOG is defined in my included variables file.
Next, wrap the command in a function call like so:
That’s better. Now let’s add a second command that sets a custom location for OS X to store screenshot files.
Add command line arguments
If you saved and ran this script now it wouldn’t do anything. That’s because you have to tell the script which functions to execute. I want to be able to cherry pick which functions in my script to run via the command line. We can do this with a Bash utility called “getopts”.
It looks kinda scary but it’s really not. What we’ve done is assign 2 command line arguments and linked them to corresponding function names. Here’s what the entire completed script looks like:
Download the raw file to your machine and try running it with with no command line arguments. You’ll receive a helpful warning message that says “You did not enter a value. -o = Dump list of outdated HomeBrew packages; -s Changes location of saved screenshots.” Now, rerun it but this time specify “myscript.sh -o” argument. You can also daisy chain them together like so “myscript.sh -os”.
Adding new functions to the script is easy, and adding new “getopts” cases for each new function is equally easy. Just copy the “s ) screenshots ;;” row, change the new argument letter or symbol as well as the name of the function. Don’t forget to add the new argument option to the line “while getopts “:os” Option”, where your new argument symbol goes after the colon.