ARR

A bash at bash scripting

Posted on Nov 23, 2022

For all the years I’ve been in IT, I’ve never really scripted a lot of things to automate tasks I perform regularly.

That’s not entirely true, I guess… I use Ansible daily at work, creating or modifing roles that are responsible for automating tasks on various machines across the department, but when it comes to simple local tasks, I’ve kinda just stuck to either creating aliases or functions that I source with my {bash,zsh}rc file or running something from the history file.

Okay, it turns out I kinda script a bunch of things, but the point is that I want to challenge myself to create a bash script every day or so just to keep myself in the flow of things. These don’t need to be complicated, and ideally stick to one task only. I’m also using this as a learning experience to pick up on best practices rather than just throwing something together because “it works for me”.

Today’s script, for example:

#!/usr/bin/env bash

# 2022-11-23

set -o errexit
set -o nounset
set -o pipefail

if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
  echo 'Usage: ./wttr.sh optional-argument

  If an argument is provided, the wttr.in/$argument will be queried.
  
  e.g. % ./wttr.sh paisley

  When no arguement is provided, it will default to wttr.in/gla.
  '
  exit
fi

if [[ $# -eq 0 ]]; then
  curl wttr.in/gla
else
  curl wttr.in./"$1"
fi

Let’s see how this goes!