Post #2206253
2026-04-06 17:39 UTC
Sorry, late to the party. I like to use variations of them for debugging:
```sh
red="$(tput setaf 1)"
#green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
reset="$(tput sgr0)"
messg() { printf '%-20s\t%s\n' "$1" "$2"; }
warn() { messg "${yellow}Warn${reset}:" "$1"; }
error() { messg "${red}Error:" "$1" >&2; [ -n "$2" ] && exit "$2"; } # complain to STDERR and exit if given code
debug() { [ -n "$debug" ] && messg "${blue}$1${reset}" "$2"; }
[ "$debug" = "verbose" ] && set -x
if [ -n "$debug" ]; then
alias rm='rm -v'
alias mv='mv -v'
alias mkdir='mkdir -v'
alias ln='ln -v'
fi
```
And those for config/state management:
```sh
get_state() { grep "$1" "$state_file" 2>/dev/null |cut -d':' -f2; }
set_state() { printf '%s:%s\n' "$1" "$2" >> "$state_file"; }
get_conf() { cut -d'#' -f1 "$config_file" |grep -- "$1" |cut -d"=" -f2- ; }
set_conf() {
#: gracefully sets config options
#: meaning: changes value if key exists, creates it if not
key="$1" value="$2" config="$config_file"
if grep -Eq "${key}=" "$config"
then sed -Ei "s/$key=.*/$key=$value/g" "$config"
else printf '%s=%s\n' "$key" "$value" >> "$config"
fi
}
```
And a few more:
```sh
isexec() { [ -x "$(command -v "$1" 2>/dev/null)" ]; }
contains() { case "$1" in *${2}*) return 0;; *) return 1;; esac; }
```
Replies (0)
No replies.