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 / is
Created May 15, 2020 10:56
A variant of Jozef Sokolowski's is.sh
is() {
[ "${1}" = "not" ] && shift 1; ! is "${@}"; return "${?}"
_condition="${1}"; _var_a="${2}"; _var_b="${3}"
case "${_condition}" in
(-[bcdefghLnprsStuwxz])
test "${_condition}" "${_var_a}"; return "${?}"
;;
(command) command -v "${_var_a}"; return "${?}" ;;
(file) [ -f "${_var_a}" ]; return "${?}" ;;
(dir|directory) [ -d "${_var_a}" ]; return "${?}" ;;
# Get the top level PID and setup a trap so that we can call die() within subshells
trap "exit 1" TERM
_self_pid="${$}"
export _self_pid
# Function to print an error message and exit
die() {
if [ -t 0 ]; then
printf '\e[31;1m====>%s\e[0m\n' "${0}:(${LINENO}): ${*}" >&2
else
@rawiriblundell
rawiriblundell / daikin
Created April 13, 2020 07:58
A function to get some basic state and temperature info from a Daikin heatpump
daikin() {
local remote_unit temp_info unit_info unit_mode inside_temperature
local outside_temperature target_temperature
remote_unit=$(host -4 Client.burnination.net | awk '{print $4}')
case "${remote_unit}" in
(found:)
printf -- '%s\n' "Daikin Aircon Information" \
"=========================" \
"Remote unit not found" >&2
@rawiriblundell
rawiriblundell / get_shell
Last active January 25, 2023 20:58
A function to detect the parent shell
# Because $SHELL is an unreliable thing to test against, we provide this function
# This won't work for 'fish', which needs 'ps -p %self' or similar
# non-bourne-esque syntax.
# TO-DO: Investigate application of 'export PS_PERSONALITY="posix"'
get_shell() {
if [ -r "/proc/$$/cmdline" ]; then
# We use 'tr' because 'cmdline' files have NUL terminated lines
# TO-DO: Possibly handle multi-word output e.g. 'busybox ash'
printf -- '%s\n' "$(tr '\0' ' ' </proc/"$$"/cmdline)"
elif ps -p "$$" >/dev/null 2>&1; then
@rawiriblundell
rawiriblundell / get_rc
Created January 9, 2020 11:23
A tiny function to silence commands and output their return code, useful for portability (e.g. where 'grep' might not have a '-q' arg on a classic unix)
get_rc() {
"${@}" >/dev/null 2>&1 && return "${?}"
}
@rawiriblundell
rawiriblundell / print-xterm-256color
Last active December 30, 2019 00:02
A bash function to print a terminfo for 'xterm-256color' for systems like Solaris
# This function prints the terminfo details for 'xterm-256color'
# This is for importing this into systems that don't have this
print-xterm-256color() {
cat <<'NEWTERM'
xterm-256color|xterm with 256 colors,
am, bce, ccc, km, mc5i, mir, msgr, npc, xenl,
colors#256, cols#80, it#8, lines#24, pairs#32767,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=^M,
@rawiriblundell
rawiriblundell / text-functions.sh
Last active April 3, 2020 12:38
Text transformation functions in a 'library'
# shellcheck shell=bash
# The MIT License (MIT)
# Copyright (c) 2019 -, Rawiri Blundell
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@rawiriblundell
rawiriblundell / setup_glow
Last active December 27, 2019 20:35
Setup the glow markdown viewer
#!/bin/bash
# Functionalise 'command -v' to allow 'if get_command [command]' idiom
get_command() { command -v "${1}" &>/dev/null; }
# TO-DO: Update environment to ensure a sane $ARCH variable
# This will allow us to make this portable to various arm implementations
setup_glow() {
# If it's already present, there's nothing to do here
if get_command glow; then
@rawiriblundell
rawiriblundell / mapfile
Created December 12, 2019 11:19
A function to provide the most basic mapfile capability for pre-bash4
# A portability function for older systems that don't have the mapfile builtin
if ! command -v mapfile >/dev/null 2>&1; then
mapfile() {
local _arrName i IFS
unset MAPFILE
set -f # Turn off globbing
set +H # Prevent parsing of '!' via history substitution
# We use the behaviour of '-t' by default, so if it's given, skip it
@rawiriblundell
rawiriblundell / upgrade_shellcheck
Created November 20, 2019 22:03
A script to update shellcheck when using the precompiled binary
#!/bin/bash
# A quick script to automate upgrading the shellcheck binary
# Terse assign our default proxy settings if required
#: "${http_proxy:=http://proxy.company.org:3128}"
#: "${https_proxy:=$http_proxy}"
trap "rm -rf /tmp/shellcheck-latest*" EXIT