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 / listdirs
Created November 13, 2019 06:25
Do not parse the output of ls within scripts. So... how do you list just directories then? Here's how
# Note that this uses the 'func() ( ;)' form, not 'func() { ;}'
# in order to contain any changes to dotglob or nullglob
# This means that this function will also return directories
# whose names begin with a dot.
listdirs() (
shopt -s dotglob nullglob
set -- */
printf -- '%s\n' "${@%/}"
)
#!/bin/bash
# A merge of ideas from the following sources
# https://askubuntu.com/q/476641
# https://github.com/dylanaraps/neofetch/blob/master/neofetch
# https://raw.githubusercontent.com/mintty/utils/master/terminal
# If TERM_PROGRAM is set, give it a name that we recognise
# Otherwise, poll the terminal and interpret what it feeds back
# Otherwise, try to glean the information from the PPID
@rawiriblundell
rawiriblundell / ubuntu-latest-kernel
Last active September 30, 2019 07:56
Scripted version of a reddit post detailing installing a more recent kernel
#!/bin/bash
# Scripted version of this reddit post:
# https://www.reddit.com/r/linuxmint/comments/dawx7s/how_i_fixed_my_ubuntu_base_to_get_the_best/
# Beerware Licence and standard no warranty, no liability disclaimers
# TO-DO: Perhaps insert a reboot after the kernel installation
# Define any extra packages that we want
extra_packages=(
amd64-microcode
@rawiriblundell
rawiriblundell / lightswitch
Last active October 21, 2021 09:48
Script to switch between light and dark themes for Cinammon running on Mint
#!/bin/bash
# lightswitch - Switch themes between light and dark mode
# This is intended for Linux hosts using the Cinnamon desktop e.g. Mint.
export prog_name="${0##*/}"
export app_filename="/usr/share/applications/${prog_name}.desktop"
# Set our times to switch between light and dark themes (24hr time)
lights_on="${lights_on:-0900}"
lights_off="${lights_off:-2000}"
@rawiriblundell
rawiriblundell / echo
Last active October 8, 2020 20:59
Attempt to write a portable-ish echo shell function
echo() {
case "${1}" in
(-e)
case "${2}" in
(-n|--end) shift 2; printf -- '%b' "${*}" ;;
(*) shift; printf -- '%b\n' "${*}" ;;
esac
;;
(-E)
case "${2}" in
@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
}
@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 / 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 / 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 / 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