Skip to content

Instantly share code, notes, and snippets.

@Summertime
Last active March 19, 2023 11:22
Show Gist options
  • Save Summertime/7299bf4c48fbc58eb33f6630b7ed6cc2 to your computer and use it in GitHub Desktop.
Save Summertime/7299bf4c48fbc58eb33f6630b7ed6cc2 to your computer and use it in GitHub Desktop.
Bash Book
#!/usr/bin/env bash
cat - /dev/fd/3 <<- EOF 3<<- 'EOF'
#!/usr/bin/env bash
shopt -s -o errexit nounset noclobber pipefail
BASH_COMPAT=${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}
shopt -s extglob globstar lastpipe patsub_replacement
APP_ID=$(systemd-id128 new)
EOF
BOOT_ID=$(systemd-id128 boot-id --app-specific="$APP_ID")
MACHINE_ID=$(systemd-id128 machine-id --app-specific="$APP_ID")
# INVOCATION_ID=$(systemd-id128 invocation-id) # For services
: ${XDG_DATA_HOME:=$HOME/.local/share}
: ${XDG_CONFIG_HOME:=$HOME/.config}
: ${XDG_STATE_HOME:=$HOME/.local/state}
: ${XDG_DATA_DIRS:=/usr/local/share/:/usr/share/}
: ${XDG_CONFIG_DIRS:=/etc/xdg}
: ${XDG_CACHE_HOME:=$HOME/.cache}
: ${XDG_RUNTIME_DIR:=$HOME/.local/state/runtime}
declare -a TRAP_EXIT=()
trap 'for A in "${TRAP_EXIT[@]}";do eval "$A";done' EXIT
EOF
#!/usr/bin/env bash
# Don't use `! is-empty`, as it will hide errors
is-empty () {
local A=
A=$(printf '%s\0' "$@" | find -files0-from - -maxdepth 0 -empty -printf .)
if (($?)); then return 2; fi
[[ $A ]]
}
is-not-empty () {
local A=
A=$(printf '%s\0' "$@" | find -files0-from - -maxdepth 0 -not -empty -printf .)
if (($?)); then return 2; fi
[[ $A ]]
}
#!/usr/bin/env bash
is-number () {
for n in "$@"; do
# Specific numbers to avoid locale issues
if [[ $n != *[!0123456789]* ]]; then
return 1
fi
done
return 0
}
#!/usr/bin/env bash
TEMP=
TRAP_EXIT+=( 'rm -f "$TEMP"' )
TEMP=$(mktemp)
#!/usr/bin/env bash
prefix () {
declare USAGE="${FUNCNAME[0]} [-h] [-0] [--] [ARGS ...]"
declare NL='\n'
declare -a READARGS=()
declare -i OPTIND=1
while getopts h0 OPT; do
case "$OPT" in
'h')printf 'Usage: %s\n' "$USAGE";return;;
'?')printf 'Usage: %s\n' "$USAGE">&2;return 1;;
'0')NL='\0';READARGS=(-d $'\0');;
esac
done
shift $((OPTIND - 1))
while read -r "${READARGS[@]}"; do
for A; do
printf "%s$NL" "$A$REPLY"
done
done
}
suffix () {
declare USAGE="${FUNCNAME[0]} [-h] [-0] [--] [ARGS ...]"
declare NL='\n'
declare -a READARGS=()
declare -i OPTIND=1
while getopts h0 OPT; do
case "$OPT" in
'h')printf 'Usage: %s\n' "$USAGE";return;;
'?')printf 'Usage: %s\n' "$USAGE">&2;return 1;;
'0')NL='\0';READARGS=(-d $'\0');;
esac
done
shift $((OPTIND - 1))
while read -r "${READARGS[@]}"; do
for A; do
printf "%s$NL" "$REPLY$A"
done
done
}
#!/usr/bin/env bash
upper () {
declare USAGE="${FUNCNAME[0]} [-h] [--]"
declare -i OPTIND=1
while getopts h OPT; do
case "$OPT" in
'h')printf 'Usage: %s\n' "$USAGE";return;;
'?')printf 'Usage: %s\n' "$USAGE">&2;return 1;;
esac
done
shift $((OPTIND - 1))
tr '[:lower:]' '[:upper:]'
}
lower () {
declare USAGE="${FUNCNAME[0]} [-h] [--]"
declare -i OPTIND=1
while getopts h OPT; do
case "$OPT" in
'h')printf 'Usage: %s\n' "$USAGE";return;;
'?')printf 'Usage: %s\n' "$USAGE">&2;return 1;;
esac
done
shift $((OPTIND - 1))
tr '[:upper:]' '[:lower:]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment