From 435e3479fb6968c3b8d7f6f426f31a011fff536f Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 16 Jul 2023 22:22:28 -0500 Subject: nyahhhhh ~~~~~~ --- bash/functions.bash | 216 +++++++++++++++++++++++++++------------------------- bash/history.bash | 2 +- 2 files changed, 114 insertions(+), 104 deletions(-) (limited to 'bash') diff --git a/bash/functions.bash b/bash/functions.bash index 4241c8d..66724af 100644 --- a/bash/functions.bash +++ b/bash/functions.bash @@ -1,143 +1,144 @@ # Functions memq() { # memq ITEM ARRAY - ## Test whether an ITEM is a member of ARRAY. - ## Pass ARRAY as ${ARRAY[@]}. - local e needle="$1" - shift - for e; do - [[ "$e" == "$needle" ]] && { - return 0 - } - done - return 1 + ## Test whether an ITEM is a member of ARRAY. + ## Pass ARRAY as ${ARRAY[@]}. + local e needle="$1" + shift + for e; do + [[ "$e" == "$needle" ]] && { + return 0 + } + done + return 1 } rebashrc() { # rebashrc - ## Reload ~/.bashrc - printf "Loading ~/.bashrc..." >&2 - if source "$HOME/.bashrc"; then - echo "OK." >&2 - else - echo "ERROR!" >&2 - fi + ## Reload ~/.bashrc + printf "Loading ~/.bashrc..." >&2 + if source "$HOME/.bashrc"; then + echo "OK." >&2 + else + echo "ERROR!" >&2 + fi } first_which() { # first_which COMMAND... - ## Return the fully-qualified path of the first COMMAND found in $PATH. - while :; do - command -v "$1" && break - [ -z "$1" ] && return 1 - shift - done + ## Return the fully-qualified path of the first COMMAND found in $PATH. + while :; do + command -v "$1" && break + [ -z "$1" ] && return 1 + shift + done } please() { # please [COMMAND...] - # if run without arguments, run the last command with 'sudo' (aka sudo !!) - # if run WITH arguments, alias as sudo - history -d -1 - if [ -z "$1" ]; then - #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q') - set -- $(fc -lnr | sed 1q) - fi - echo >&2 sudo "$@" - history -s sudo "$@" - "${DEBUG:-false}" || sudo "$@" + # if run without arguments, run the last command with 'sudo' (aka sudo !!) + # if run WITH arguments, alias as sudo + history -d -1 + if [ -z "$1" ]; then + #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q') + set -- $(fc -lnr | sed 1q) + fi + echo >&2 sudo "$@" + history -s sudo "$@" + "${DEBUG:-false}" || sudo "$@" } mkcd() { - if [ $# -lt 1 ]; then - command cd - return "$?" - fi - if [ "x$1" = x- ]; then - command cd - - return "$?" - fi - if ! [ -d "$1" ]; then - read -p "$1 doesn't exist. Create (y/N)? " yn - case "$yn" in - n* | N*) return 1 ;; - y* | Y*) mkdir -p "$1" ;; - *) return 1 ;; - esac - fi - command cd "$1" + if [ $# -lt 1 ]; then + command cd + return "$?" + fi + if [ "x$1" = x- ]; then + command cd - + return "$?" + fi + if ! [ -d "$1" ]; then + read -p "$1 doesn't exist. Create (y/N)? " yn + case "$yn" in + n* | N*) return 1 ;; + y* | Y*) mkdir -p "$1" ;; + *) return 1 ;; + esac + fi + command cd "$1" } alias cd='mkcd ' # from tomasino # alias julian='echo "x = $(date +%s); scale=5; x / 86400 + 2440587.5" | bc' julian() { - echo "x = $(date ${1:+-d "$*"} +%s); scale=5; x / 86400 + 2440587.5" | bc + echo "x = $(date ${1:+-d "$*"} +%s); scale=5; x / 86400 + 2440587.5" | bc } # find files for pipelines f() { - find "${1:-$PWD}" -depth | - while read -r file; do - printf '%q\n' "$file" - done + find "${1:-$PWD}" -depth | + while read -r file; do + printf '%q\n' "$file" + done } words() { - grep -E "$1" /usr/share/dict/words + grep -E "$1" /usr/share/dict/words | + sed -e '/^[A-Z]/d' -e "/'s$/d" } if ! command -v dict >/dev/null 2>&1; then - dict() { - curl "dict://dict.org/d:$1" | less - } + dict() { + curl "dict://dict.org/d:$1" | less + } fi if command -v thesauracles >/dev/null 2>&1; then - thesauraphrase() { - for word in "$@"; do - thesauracles -q "$word" - done | tr '\n' ' ' - echo - } + thesauraphrase() { + for word in "$@"; do + thesauracles -q "$word" + done | tr '\n' ' ' + echo + } fi up() { - : "${UP_TODIRECTORY:=..}" - : "${UP_SPECIALARGS:=true}" - local ret=0 - # echo "$UP_TODIRECTORY" "$UP_SPECIALARGS" - if "$UP_SPECIALARGS"; then - case "$1" in - '') cd "$UP_TODIRECTORY" ;; - up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;; - --) UP_SPECIALARGS=false up "${@:2}" ;; - -*) if (( "$1" == -1 )); then - up - else - UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 )) - fi - ;; - *) while cd ..; do - case "$PWD" in - /) ret=1; break ;; - */"$1") break ;; - esac - done - ;; - esac - else - case "$1" in - '') cd "$UP_TODIRECTORY" ;; - *) while cd ..; do - case "$PWD" in - /) ret=1; break ;; - */"$1") break ;; - esac - done - ;; - esac - fi - UP_TODIRECTORY= - UP_SPECIALARGS= - return "$ret" + : "${UP_TODIRECTORY:=..}" + : "${UP_SPECIALARGS:=true}" + local ret=0 + # echo "$UP_TODIRECTORY" "$UP_SPECIALARGS" + if "$UP_SPECIALARGS"; then + case "$1" in + '') cd "$UP_TODIRECTORY" ;; + up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;; + --) UP_SPECIALARGS=false up "${@:2}" ;; + -*) if (( "$1" == -1 )); then + up + else + UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 )) + fi + ;; + *) while cd ..; do + case "$PWD" in + /) ret=1; break ;; + */"$1") break ;; + esac + done + ;; + esac + else + case "$1" in + '') cd "$UP_TODIRECTORY" ;; + *) while cd ..; do + case "$PWD" in + /) ret=1; break ;; + */"$1") break ;; + esac + done + ;; + esac + fi + UP_TODIRECTORY= + UP_SPECIALARGS= + return "$ret" } apk() { # wrapper for apk(1) @@ -160,3 +161,12 @@ mkssh() { ssh-keygen -t ed25519 -f "$HOME/.ssh/$1" cat "$HOME/.ssh/$1.pub" | tee /dev/stdout | wl-copy } + +p() { + cmd="$(history | + awk '{for(i=4;i<=NF;i++)printf "%s ",$i;print""}' | + tail -n2 | + head -n1)" + echo "$cmd" + eval $cmd +} diff --git a/bash/history.bash b/bash/history.bash index d2bdceb..4a458fb 100644 --- a/bash/history.bash +++ b/bash/history.bash @@ -29,7 +29,7 @@ HISTCONTROL=erasedups # identical to 'ignorespace'." -- info (bash)Bash Variables HISTIGNORE='&:[ ]*' # Other commands to ignore -HISTIGNORE="$HISTIGNORE:ls:exit:cd" +HISTIGNORE="$HISTIGNORE:ls:exit:cd:p" # Automatically append to HISTFILE on every command PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}" -- cgit 1.4.1-21-gabe81