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 / watch
Created May 17, 2018 00:41
Attempt to replicate 'watch' command, never used, so expelled from my .bashrc
# Check if 'watch' is available, if not, enable a stop-gap function
if ! command -v watch &>/dev/null; then
watch() {
local OPTIND colWidth titleHead sleepTime dateNow
while getopts ":hn:vt" optFlags; do
case "${optFlags}" in
(h) printf '%s\n' "Usage:" " watch [-hntv] <command>" "" \
"Options:" \
" -h, help. Print a summary of the options" \
@rawiriblundell
rawiriblundell / touch
Created May 17, 2018 00:40
Function that merges 'touch' and 'mkdir -p'. This overlays the two, hence: 'touch -p'
# Add -p option to 'touch' to combine 'mkdir -p' and 'touch'
# The trick here is that we use 'command' to launch 'touch',
# as it overrides the shell's lookup order.. essentially speaking.
touch() {
# Check if '-p' is present.
# For bash3+ you could use 'if [[ "$@" =~ -p ]];'
if echo "$@" | grep "\\-p" >/dev/null 2>&1; then
# Transfer everything to a local array
local argArray=( "$@" )
@rawiriblundell
rawiriblundell / flocate
Created May 16, 2018 23:47
Function that streamlines the regex ability of 'locate'
# flocate function. This gives a search function that blends find and locate
# Will obviously only work where locate lives, so Solaris will mostly be out of luck
# Usage: flocate searchterm1 searchterm2 searchterm[n]
# Source: http://solarum.com/v.php?l=1149LV99
flocate() {
if ! command -v locate &>/dev/null; then
printf '%s\n' "[ERROR]: 'flocate' depends on 'locate', which wasn't found."
return 1
fi
if (( $# > 1 )); then
@rawiriblundell
rawiriblundell / full-larson
Created May 10, 2018 03:15
Print any number of stars and use font effects (dim, normal, bold) to give the effect of progress, goes from side to side
#!/bin/bash
progWidth="${1:-3}"
sleepTime="0.2"
resetTerm() {
tput sgr0 # Unset as many things that we've set as possible
tput cnorm # Display the cursor again
printf '%s\n' ""
exit 0
@rawiriblundell
rawiriblundell / half-larson
Created May 10, 2018 03:14
Print any number of stars and use font effects (dim, normal, bold) to give the effect of progress, left to right
#!/bin/bash
progWidth="${1:-3}"
sleepTime="0.2"
resetTerm() {
tput sgr0 # Unset as many things that we've set as possible
tput cnorm # Display the cursor again
printf '%s\n' ""
exit 0
@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() {
@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 / 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 / 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 / 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