Beginning to play with shell scripting
I had originally worried that when I started learning Docker, Ansible and Terraform that I'd get much weaker with my shell skills, or at least stop improving. The completely opposite has been true: I've learned much more about Linux and the shell than I've ever known from using these tools. While I'm still new to all of these tools and I'm novice at shell scripting, it strikes me that I've learned more about development and operations than I ever reasonably imagined I would when I started programming five years ago. Wild.
Shell scripting notes
- Variables are global by default, but you can use the
localkeyword inside of functions to scope variables to that function.
shiftcommand nukes the first parameter that was passed through the shell. It's janky compared to named parameters. I messed this up a bunch causing weird errors
- Commands like
getoptscreate magic global variables like
$OPTARG. For magic variables like
$OPTIND, you may want to explicitly declare it as
local OPTIND=1within a function because it doesn't automatically reset itself (I think).
- I need to use quotes to reliably test strings for zero characters, using
[ -z "$variable" ]instead of
[ -z $variable ].
- Even though
ifis closed with
caseis closed with
dois closed with
- The whole
do-something > /dev/null 2>&1always messed me up because it looked like I was redirecting
/dev/nullto standard out, but apparently it goes back to the start and sends everything to
- You shouldn't use
whichin shell scripts apparently. You actually want to use
command -v executable. Stack Exchange has a thorough writeup on this.
- Functions don't hoist in Bash. I'm not surpised, but it kind of messes up my flow. I ended up creating a
mainfunction at the top and call it at the very bottom of the script. The whole script loads before
mainis called, so it works just fine when the function uses other functions declared below it.
I'm incredibly tired, so I'm going to hit published and go to bed without proofreading. Typos are freedm.
I published this and was getting ready to go to bed, but then I realized that I forgot to mention: I got Terraform all set up to provision my DigitalOcean infrastructure. And it works inside a container using the Bash CLI that I built.
Here are what I think my next few steps are for SacMusic to get to the top of the mountain:
- Create an Ansible playbook for configuring newly provisioned server.
- Make Jenkins pipeline tag newly built images as
latestin addition to their current tags.
- Create a playbook that dynamically grabs DigitalOcean inventory and deploys the latest images to the server.
- Put the playbook in my Jenkins deployment pipeline.
- Configure DataDog for logging.
- Configure Rollbar (or maybe Sentry) for error monitoring.
- Tighten up my Nginx config.
- Maybe build out my tests.
Once I knock these out, I'll have basically met my goals for leveling up my DevOps skills to where I want them to be (for now). After this, I plan to switch to just-in-time learning for DevOps, picking up new skills as I need them and not before. This rabbit hole goes too deep, and the tools I'm learning solve most of the technical pain points from my last job.
Focusing on DevOps has caused me to stagnate on application development, which is what I'm actually passionate about. And none of the things I'm building enrich people's lives (though these skills will certainly make my life more tolerable).
I'm eager to resume building things that break free of the glowing rectangle. I'm not sure what those things are yet. But I'm excited to find out.