diff options
Diffstat (limited to 'bash')
-rw-r--r-- | bash/functions.bash | 216 | ||||
-rw-r--r-- | bash/history.bash | 2 |
2 files changed, 114 insertions, 104 deletions
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 @@ | |||
1 | # Functions | 1 | # Functions |
2 | 2 | ||
3 | memq() { # memq ITEM ARRAY | 3 | memq() { # memq ITEM ARRAY |
4 | ## Test whether an ITEM is a member of ARRAY. | 4 | ## Test whether an ITEM is a member of ARRAY. |
5 | ## Pass ARRAY as ${ARRAY[@]}. | 5 | ## Pass ARRAY as ${ARRAY[@]}. |
6 | local e needle="$1" | 6 | local e needle="$1" |
7 | shift | 7 | shift |
8 | for e; do | 8 | for e; do |
9 | [[ "$e" == "$needle" ]] && { | 9 | [[ "$e" == "$needle" ]] && { |
10 | return 0 | 10 | return 0 |
11 | } | 11 | } |
12 | done | 12 | done |
13 | return 1 | 13 | return 1 |
14 | } | 14 | } |
15 | 15 | ||
16 | rebashrc() { # rebashrc | 16 | rebashrc() { # rebashrc |
17 | ## Reload ~/.bashrc | 17 | ## Reload ~/.bashrc |
18 | printf "Loading ~/.bashrc..." >&2 | 18 | printf "Loading ~/.bashrc..." >&2 |
19 | if source "$HOME/.bashrc"; then | 19 | if source "$HOME/.bashrc"; then |
20 | echo "OK." >&2 | 20 | echo "OK." >&2 |
21 | else | 21 | else |
22 | echo "ERROR!" >&2 | 22 | echo "ERROR!" >&2 |
23 | fi | 23 | fi |
24 | } | 24 | } |
25 | 25 | ||
26 | first_which() { # first_which COMMAND... | 26 | first_which() { # first_which COMMAND... |
27 | ## Return the fully-qualified path of the first COMMAND found in $PATH. | 27 | ## Return the fully-qualified path of the first COMMAND found in $PATH. |
28 | while :; do | 28 | while :; do |
29 | command -v "$1" && break | 29 | command -v "$1" && break |
30 | [ -z "$1" ] && return 1 | 30 | [ -z "$1" ] && return 1 |
31 | shift | 31 | shift |
32 | done | 32 | done |
33 | } | 33 | } |
34 | 34 | ||
35 | please() { # please [COMMAND...] | 35 | please() { # please [COMMAND...] |
36 | # if run without arguments, run the last command with 'sudo' (aka sudo !!) | 36 | # if run without arguments, run the last command with 'sudo' (aka sudo !!) |
37 | # if run WITH arguments, alias as sudo | 37 | # if run WITH arguments, alias as sudo |
38 | history -d -1 | 38 | history -d -1 |
39 | if [ -z "$1" ]; then | 39 | if [ -z "$1" ]; then |
40 | #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q') | 40 | #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q') |
41 | set -- $(fc -lnr | sed 1q) | 41 | set -- $(fc -lnr | sed 1q) |
42 | fi | 42 | fi |
43 | echo >&2 sudo "$@" | 43 | echo >&2 sudo "$@" |
44 | history -s sudo "$@" | 44 | history -s sudo "$@" |
45 | "${DEBUG:-false}" || sudo "$@" | 45 | "${DEBUG:-false}" || sudo "$@" |
46 | } | 46 | } |
47 | 47 | ||
48 | mkcd() { | 48 | mkcd() { |
49 | if [ $# -lt 1 ]; then | 49 | if [ $# -lt 1 ]; then |
50 | command cd | 50 | command cd |
51 | return "$?" | 51 | return "$?" |
52 | fi | 52 | fi |
53 | if [ "x$1" = x- ]; then | 53 | if [ "x$1" = x- ]; then |
54 | command cd - | 54 | command cd - |
55 | return "$?" | 55 | return "$?" |
56 | fi | 56 | fi |
57 | if ! [ -d "$1" ]; then | 57 | if ! [ -d "$1" ]; then |
58 | read -p "$1 doesn't exist. Create (y/N)? " yn | 58 | read -p "$1 doesn't exist. Create (y/N)? " yn |
59 | case "$yn" in | 59 | case "$yn" in |
60 | n* | N*) return 1 ;; | 60 | n* | N*) return 1 ;; |
61 | y* | Y*) mkdir -p "$1" ;; | 61 | y* | Y*) mkdir -p "$1" ;; |
62 | *) return 1 ;; | 62 | *) return 1 ;; |
63 | esac | 63 | esac |
64 | fi | 64 | fi |
65 | command cd "$1" | 65 | command cd "$1" |
66 | } | 66 | } |
67 | alias cd='mkcd ' | 67 | alias cd='mkcd ' |
68 | 68 | ||
69 | # from tomasino | 69 | # from tomasino |
70 | # alias julian='echo "x = $(date +%s); scale=5; x / 86400 + 2440587.5" | bc' | 70 | # alias julian='echo "x = $(date +%s); scale=5; x / 86400 + 2440587.5" | bc' |
71 | julian() { | 71 | julian() { |
72 | echo "x = $(date ${1:+-d "$*"} +%s); scale=5; x / 86400 + 2440587.5" | bc | 72 | echo "x = $(date ${1:+-d "$*"} +%s); scale=5; x / 86400 + 2440587.5" | bc |
73 | } | 73 | } |
74 | 74 | ||
75 | # find files for pipelines | 75 | # find files for pipelines |
76 | f() { | 76 | f() { |
77 | find "${1:-$PWD}" -depth | | 77 | find "${1:-$PWD}" -depth | |
78 | while read -r file; do | 78 | while read -r file; do |
79 | printf '%q\n' "$file" | 79 | printf '%q\n' "$file" |
80 | done | 80 | done |
81 | } | 81 | } |
82 | 82 | ||
83 | words() { | 83 | words() { |
84 | grep -E "$1" /usr/share/dict/words | 84 | grep -E "$1" /usr/share/dict/words | |
85 | sed -e '/^[A-Z]/d' -e "/'s$/d" | ||
85 | } | 86 | } |
86 | 87 | ||
87 | if ! command -v dict >/dev/null 2>&1; then | 88 | if ! command -v dict >/dev/null 2>&1; then |
88 | dict() { | 89 | dict() { |
89 | curl "dict://dict.org/d:$1" | less | 90 | curl "dict://dict.org/d:$1" | less |
90 | } | 91 | } |
91 | fi | 92 | fi |
92 | 93 | ||
93 | if command -v thesauracles >/dev/null 2>&1; then | 94 | if command -v thesauracles >/dev/null 2>&1; then |
94 | thesauraphrase() { | 95 | thesauraphrase() { |
95 | for word in "$@"; do | 96 | for word in "$@"; do |
96 | thesauracles -q "$word" | 97 | thesauracles -q "$word" |
97 | done | tr '\n' ' ' | 98 | done | tr '\n' ' ' |
98 | echo | 99 | echo |
99 | } | 100 | } |
100 | fi | 101 | fi |
101 | 102 | ||
102 | up() { | 103 | up() { |
103 | : "${UP_TODIRECTORY:=..}" | 104 | : "${UP_TODIRECTORY:=..}" |
104 | : "${UP_SPECIALARGS:=true}" | 105 | : "${UP_SPECIALARGS:=true}" |
105 | local ret=0 | 106 | local ret=0 |
106 | # echo "$UP_TODIRECTORY" "$UP_SPECIALARGS" | 107 | # echo "$UP_TODIRECTORY" "$UP_SPECIALARGS" |
107 | if "$UP_SPECIALARGS"; then | 108 | if "$UP_SPECIALARGS"; then |
108 | case "$1" in | 109 | case "$1" in |
109 | '') cd "$UP_TODIRECTORY" ;; | 110 | '') cd "$UP_TODIRECTORY" ;; |
110 | up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;; | 111 | up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;; |
111 | --) UP_SPECIALARGS=false up "${@:2}" ;; | 112 | --) UP_SPECIALARGS=false up "${@:2}" ;; |
112 | -*) if (( "$1" == -1 )); then | 113 | -*) if (( "$1" == -1 )); then |
113 | up | 114 | up |
114 | else | 115 | else |
115 | UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 )) | 116 | UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 )) |
116 | fi | 117 | fi |
117 | ;; | 118 | ;; |
118 | *) while cd ..; do | 119 | *) while cd ..; do |
119 | case "$PWD" in | 120 | case "$PWD" in |
120 | /) ret=1; break ;; | 121 | /) ret=1; break ;; |
121 | */"$1") break ;; | 122 | */"$1") break ;; |
122 | esac | 123 | esac |
123 | done | 124 | done |
124 | ;; | 125 | ;; |
125 | esac | 126 | esac |
126 | else | 127 | else |
127 | case "$1" in | 128 | case "$1" in |
128 | '') cd "$UP_TODIRECTORY" ;; | 129 | '') cd "$UP_TODIRECTORY" ;; |
129 | *) while cd ..; do | 130 | *) while cd ..; do |
130 | case "$PWD" in | 131 | case "$PWD" in |
131 | /) ret=1; break ;; | 132 | /) ret=1; break ;; |
132 | */"$1") break ;; | 133 | */"$1") break ;; |
133 | esac | 134 | esac |
134 | done | 135 | done |
135 | ;; | 136 | ;; |
136 | esac | 137 | esac |
137 | fi | 138 | fi |
138 | UP_TODIRECTORY= | 139 | UP_TODIRECTORY= |
139 | UP_SPECIALARGS= | 140 | UP_SPECIALARGS= |
140 | return "$ret" | 141 | return "$ret" |
141 | } | 142 | } |
142 | 143 | ||
143 | apk() { # wrapper for apk(1) | 144 | apk() { # wrapper for apk(1) |
@@ -160,3 +161,12 @@ mkssh() { | |||
160 | ssh-keygen -t ed25519 -f "$HOME/.ssh/$1" | 161 | ssh-keygen -t ed25519 -f "$HOME/.ssh/$1" |
161 | cat "$HOME/.ssh/$1.pub" | tee /dev/stdout | wl-copy | 162 | cat "$HOME/.ssh/$1.pub" | tee /dev/stdout | wl-copy |
162 | } | 163 | } |
164 | |||
165 | p() { | ||
166 | cmd="$(history | | ||
167 | awk '{for(i=4;i<=NF;i++)printf "%s ",$i;print""}' | | ||
168 | tail -n2 | | ||
169 | head -n1)" | ||
170 | echo "$cmd" | ||
171 | eval $cmd | ||
172 | } | ||
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 | |||
29 | # identical to 'ignorespace'." -- info (bash)Bash Variables | 29 | # identical to 'ignorespace'." -- info (bash)Bash Variables |
30 | HISTIGNORE='&:[ ]*' | 30 | HISTIGNORE='&:[ ]*' |
31 | # Other commands to ignore | 31 | # Other commands to ignore |
32 | HISTIGNORE="$HISTIGNORE:ls:exit:cd" | 32 | HISTIGNORE="$HISTIGNORE:ls:exit:cd:p" |
33 | 33 | ||
34 | # Automatically append to HISTFILE on every command | 34 | # Automatically append to HISTFILE on every command |
35 | PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}" | 35 | PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}" |