Skip to content

Instantly share code, notes, and snippets.

View rawiriblundell's full-sized avatar

Rawiri Blundell rawiriblundell

  • Wellington, New Zealand
View GitHub Profile
@rawiriblundell
rawiriblundell / SH_LIBPATH
Last active January 15, 2022 19:44
SH_LIBPATH bootstrap
# shellcheck shell=ksh
# If SH_LIBPATH is not set or null, then we try to build it
if [ -z "${SH_LIBPATH+x}" ] || [ "${#SH_LIBPATH}" -eq "0" ]; then
for _path in /usr/local/lib/sh "${HOME}"/.local/lib/sh; do
[ -d "${_path}" ] && SH_LIBPATH="${SH_LIBPATH}:${_path}"
done
fi
unset -v _path
# Remove any leading colons from the construction process and export
SH_LIBPATH="${SH_LIBPATH#:}"
@rawiriblundell
rawiriblundell / software::fetch
Created December 19, 2020 22:07
Fetch a download from a redirected URL
software::fetch() {
local local_target remote_target
remote_target="${1:?No target specified}"
remote_target="$(curl "${remote_target}" -s -L -I -o /dev/null -w '%{url_effective}')"
local_target="${remote_target##*/}"
printf -- '%s\n' "Attempting to fetch ${remote_target}..."
curl "${remote_target}" > "${local_dir}"/"${local_target}" || return 1
}
@rawiriblundell
rawiriblundell / confirm
Last active January 12, 2022 09:03
Simple one-char-triggered request for confirmation
# A function to prompt/read an interactive y/n response
# Stops reading after one character, meaning only 'y' or 'Y' will return 0
# _anything_ else will return 1
confirm() {
read -rn 1 -p "${*:-Continue} [Y/N]? "
printf -- '%s\n' ""
case "${REPLY}" in
([yY]) return 0 ;;
(*) return 1 ;;
esac
@rawiriblundell
rawiriblundell / set_env_path
Created December 13, 2020 21:55
Dynamically build PATH variable with a function that can also be used to append to the PATH
# A function to update the PATH variable
# shellcheck disable=SC2120
set_env_path() {
local path dir newPath
# If we have any args, feed them into ~/.pathrc
if (( "${#}" > 0 )); then
# shellcheck disable=SC2048
for path in ${*}; do
if [[ -d "${path}" ]]; then
@rawiriblundell
rawiriblundell / brew::is_installed
Created October 21, 2020 21:29
Function to check if a package managed by homebrew is installed
brew::is_installed() {
local failcount
failcount=0
if ! command -v brew >/dev/null 2>&1; then
printf -- '%s\n' "This script requires brew on a mac. This wasn't found..." >&2
exit 1
fi
for brew_pkg in ${*:?Package unspecified}; do
if brew list | grep -w "${brew_pkg}" >/dev/null 2>&1; then
printf -- '%s\n' "${brew_pkg} appears to be installed"
@rawiriblundell
rawiriblundell / check_idle_console
Last active October 8, 2020 10:50
checkmk local check for checking that the console hasn't been left logged in
#!/bin/bash
# check_idle_console - report on root capable interfaces with long sessions
# Purpose:
# This script tries to ensure that people don't leave ilo's logged in as root
# Author: Rawiri Blundell
# Copyright: See provided LICENCE file
###############################################################################
# Source the config mapping library
# Provides variables "${thisHost}", "${thisJob}" and
# positional parameters as "${arg1}" "${arg2}" etc.
@rawiriblundell
rawiriblundell / check_user_lastseen
Created September 13, 2020 00:13
checkmk local check for local account auditing
#!/bin/bash
# check_user_lastseen - report on users who have not logged in for a while
# Purpose:
# This script tries to find idle accounts and any orphaned homedirs
# Currently is Linux biased but capacity for portability is there
# Author: Rawiri Blundell
# Copyright: See provided LICENCE file
###############################################################################
# Source the config mapping library
# Provides variables "${thisHost}", "${thisJob}" and
@rawiriblundell
rawiriblundell / cinnamon_rand_wallpaper
Created August 28, 2020 10:40
A script I modified for someone to randomise their Cinnamon wallpaper
#!/bin/bash
# Script to set a per workspace desktop background in Cinnamon.
# Save as ~/bin/workspace_backgrounds_switcher.sh or
# ~/.local/bin/workspace_backgrounds_switcher.sh and make executable
# Add an entry in startup applications to launch the script on start-up.
# Set path to background images
background_path=/usr/share/backgrounds/linuxmint/
# Select a random background from the aforementioned path
@rawiriblundell
rawiriblundell / ipv4::validate_addr
Last active August 20, 2020 23:04
A function to validate whether an input is a valid ipv4 address
ipv4::validate_addr() {
# Disable SC2086 for 'set -- ${*%/*}' as we require this to be word split
# shellcheck disable=SC2086
(
IFS=.; set -f; set -- ${*//\"/}; set -- ${*%/*}
local octet; local count=0; local errcount=0
if (( "${#}" == 4 )); then
for octet in "${@}"; do
(( ++count ))
case "${octet}" in
@rawiriblundell
rawiriblundell / reman
Created May 15, 2020 11:04
A function for searching man pages
# Regular Expression MANual page contents search
# Maybe I should have called this 'manscape'
reman() {
local width=$(( "${COLUMNS:-$(tput cols)}" - 10 ))
(( width > "${MANWIDTH:-$width}" )) && width="${MANWIDTH}"
export MANWIDTH="${width}"
case "${1}" in
(''|-h|--help)
printf -- '%s\n' \