Skip to content

Instantly share code, notes, and snippets.

@Summertime
Last active November 23, 2022 11:57
Show Gist options
  • Save Summertime/3d143c44c41fd74e7069e0ab135434ec to your computer and use it in GitHub Desktop.
Save Summertime/3d143c44c41fd74e7069e0ab135434ec to your computer and use it in GitHub Desktop.
Bunch of functions to make things easier
#!/usr/bin/env bash
p () {
declare USAGE="${FUNCNAME[0]} [-h] [-0] [--] [ARGS ...]"
declare NL='\n'
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';;
esac
done
shift $((OPTIND - 1))
printf "%s$NL" "$@"
}
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:]'
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment