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 / 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 / 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 / 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 / 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 / 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 / pwcheck
Created May 17, 2018 00:42
Attempt to create a basic password quality check, never used, so expelled from my .bashrc
# Password strength check function. Can be fed a password most ways.
# TO-DO: add a verbose output switch
pwcheck () {
# Read password in, if it's blank, prompt the user
if [[ "${*}" = "" ]]; then
read -resp $'Please enter the password/phrase you would like checked:\n' PwdIn
else
# Otherwise, whatever is fed in is the password to check
PwdIn="${*}"
fi
@rawiriblundell
rawiriblundell / tput
Last active November 13, 2019 04:06
Overlay tput to make it work portably
# Detect if our version of 'tput' is so old that it uses termcap syntax
# If this is the case, overlay it so that newer terminfo style syntax works
# Inspired by 'bashlib' and 'liquidprompt'
# For performance we only implement if 'tput ce' (a harmless test) works
if tput ce 2>/dev/null; then
tput() {
ctput-null() { command tput "${@}" 2>/dev/null; }
ctput() { command tput "${@}"; }
case "${1}" in
(blink) ctput-null blink || ctput mb;;
@rawiriblundell
rawiriblundell / setprompt
Last active April 11, 2021 07:35
A .bashrc fragment with all required code for setprompt - a PS1 colouriser and state changer
# shellcheck shell=bash
################################################################################
# setprompt and related/required functions
################################################################################
# Wrap 'cd' to automatically update GIT_BRANCH when necessary
cd() {
command cd "${@}" || return 1
if is_gitdir; then
PS1_GIT_MODE=True
@rawiriblundell
rawiriblundell / tolower
Last active February 8, 2020 07:53
Pure bash tolower example
if (( BASH_VERSINFO >= 4 )); then
tolower() {
# If parameter is a file, or stdin is used, action that first
if [[ -r "${1}" ]]||[[ ! -t 0 ]]; then
# We structure our while read loop to handle no newline at EOF
eof=
while [[ -z "${eof}" ]]; do
read -r || eof=true
printf -- '%s\n' "${REPLY,,}"
done < "${1:-/dev/stdin}"
@rawiriblundell
rawiriblundell / logname
Created July 24, 2018 09:32
Another function that demonstrates the overlay trick of using 'command'. This adds a '--home' arg to the logname command
logname() {
case "${1}" in
(--home)
getent passwd "$(command logname)" | awk -F ':' '{print $6}'
;;
(*)
command logname "${@}"
;;
esac
}