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 / lcgrng
Created October 13, 2016 11:36
Linear congruential generator (POSIX-ish shell)
#!/bin/bash
# Linear congruential generator, range 0 - 32767
# See: https://rosettacode.org/wiki/Linear_congruential_generator
# Usage: lcgrng (optional: number count, default:1) (optional: seed, default: epoch in seconds)
rnCount="${1:-1}"
rnSeed="${2:-$(date +%s)}"
# Here's an unnecessary nod to FreeBSD's rand.c
if [ "${rnSeed}" = 0 ]; then
@rawiriblundell
rawiriblundell / lcgrng2
Created May 11, 2017 09:18
Linear congruential generator (POSIX-ish shell)
#! /bin/bash
# If the special variable isn't available (e.g. dash shell), we fall back to a BSD-style
# Linear congruential generator (LCG) which we use to create our own '$RANDOM' variable
# See: https://rosettacode.org/wiki/Linear_congruential_generator
# If two invocations of RANDOM are the same (or both are blank),
# then we're working with a shell that does not support $RANDOM.
if [ "${RANDOM}" = "${RANDOM}" ]; then
@rawiriblundell
rawiriblundell / host2ip
Created September 20, 2017 08:35
Unused/untested function for converting hostnames to ip and the reverse
host2ip() {
# If $1 is missing, print brief usage
if [[ -n "$1" ]]; then
printf '%s\n' "Usage: host2ip [hostname|ip.add.re.ss]"
return 1
# If $1 appears to be a node name, figure out its IP
elif [[ "$1" =~ '^[a-zA-Z]$' ]]; then
host -4 -W 1 "$1" | awk '{print $4}'
# If $1 appears to be an IP address, try to figure the reverse
elif [[ "$1" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; then
@rawiriblundell
rawiriblundell / xorshift128plus
Last active January 11, 2018 03:47
An implementation of a xorshift128+ RNG in shell code
#!/bin/bash
#set -x
# Function to generate a reliable seed for whatever method requires one
# Because 'date +%s' isn't entirely portable, we try other methods to get the epoch
Fn_getSeed() {
# First we check if /dev/urandom is available.
# We used to have a method_urandom. /dev/urandom can generate numbers fast
# But selecting numbers in ranges etc made for a fairly slow method
if [ -c /dev/urandom ] && command -v od >/dev/null 2>&1; then
@rawiriblundell
rawiriblundell / dst
Created March 8, 2018 02:29
Figure out the days where daylight savings will change
# Function to figure out daylight savings dates for the current year
dst() {
local TZ
# RHEL7 / systemd
if command -v timedatectl >/dev/null 2>&1; then
TZ=$(timedatectl status | awk -F ': ' '/Time zone/{print $2}' | awk '{print $1}')
# RHEL5, RHEL6 and similar aged CentOS/Fedora/etc
elif [[ -r /etc/sysconfig/clock ]]; then
TZ=$(awk -F "=" '/ZONE/{print $2}' /etc/sysconfig/clock)
# Older Debian family
@rawiriblundell
rawiriblundell / cowsay
Created March 8, 2018 23:40
A function to provide cowsay functionality on hosts that don't have it
if ! command -v cowsay &>/dev/null; then
cowsay() {
local msg
if [[ -t 0 ]] && [[ -z $1 ]]; then
msg="Moo! What should I say?"
elif [[ -n $1 ]]; then
msg="$*"
else
msg=$(paste -sd ' ' - | sed -e "s/[[:space:]]\\+/ /g")
fi
@rawiriblundell
rawiriblundell / pretty
Created May 10, 2018 02:55
Randomly colourise each line of output
pretty () {
while read -r; do
printf "\033[38;5;%dm%s\033[0m\n" $(($RANDOM%255)) "${REPLY}";
done
}
@rawiriblundell
rawiriblundell / date
Created May 10, 2018 02:56
Add ordinal date representation i.e. Mar 2 => Mar 2nd
date() {
case "$@" in
(*"%o"*)
declare -a args1
declare -a args2
while [[ -n "$1" ]]; do
args2+=("$1")
if [[ "${1:0:1}" != + ]]; then
args1+=("$1")
fi
@rawiriblundell
rawiriblundell / getAD-User-Info
Created May 10, 2018 03:10
Poll the sssd cache for locally stored information about a user. Usernames are case sensitive!
getAD-User-Info() {
local userName adUserName
userName="${1:?Username not supplied}"
# Poll 'id' to either ensure the info is cached
# or to error out if the user doesn't exist
if ! id "${userName}" &>/dev/null; then
printf -- '%s\n' "User '${userName}' could not be found"
return 1
fi
@rawiriblundell
rawiriblundell / get-cmdlist
Created May 10, 2018 03:11
Print a list of all available commands and a short description about them
#!/bin/bash
# Because you never know what crazy systems are out there
if ! command -v apropos >/dev/null 2>&1; then
apropos() { man -k "$*"; }
fi
# A small function to trim whitespace either side of a (sub)string
# shellcheck disable=SC2120
trim() {