about summary refs log tree commit diff stats
path: root/bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash')
-rw-r--r--bash/functions.bash216
-rw-r--r--bash/history.bash2
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
3memq() { # memq ITEM ARRAY 3memq() { # 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
16rebashrc() { # rebashrc 16rebashrc() { # 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
26first_which() { # first_which COMMAND... 26first_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
35please() { # please [COMMAND...] 35please() { # 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
48mkcd() { 48mkcd() {
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}
67alias cd='mkcd ' 67alias 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'
71julian() { 71julian() {
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
76f() { 76f() {
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
83words() { 83words() {
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
87if ! command -v dict >/dev/null 2>&1; then 88if ! 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 }
91fi 92fi
92 93
93if command -v thesauracles >/dev/null 2>&1; then 94if 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 }
100fi 101fi
101 102
102up() { 103up() {
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
143apk() { # wrapper for apk(1) 144apk() { # 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
165p() {
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
30HISTIGNORE='&:[ ]*' 30HISTIGNORE='&:[ ]*'
31# Other commands to ignore 31# Other commands to ignore
32HISTIGNORE="$HISTIGNORE:ls:exit:cd" 32HISTIGNORE="$HISTIGNORE:ls:exit:cd:p"
33 33
34# Automatically append to HISTFILE on every command 34# Automatically append to HISTFILE on every command
35PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}" 35PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}"