about summary refs log tree commit diff stats
path: root/bash/functions.bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash/functions.bash')
-rw-r--r--bash/functions.bash166
1 files changed, 109 insertions, 57 deletions
diff --git a/bash/functions.bash b/bash/functions.bash index 64e8001..97f89b8 100644 --- a/bash/functions.bash +++ b/bash/functions.bash
@@ -1,89 +1,141 @@
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 "$1" /usr/share/dict/words 84 grep "$1" /usr/share/dict/words
85} 85}
86 86
87dict() { 87if ! command -v dict >/dev/null 2>&1; then
88 curl "dict://dict.org/d:$1" | less 88 dict() {
89 curl "dict://dict.org/d:$1" | less
90 }
91fi
92
93if command -v thesauracles >/dev/null 2>&1; then
94 thesauraphrase() {
95 for word in "$@"; do
96 thesauracles -q "$word"
97 done | tr '\n' ' '
98 echo
99 }
100fi
101
102up() {
103 : "${UP_TODIRECTORY:=..}"
104 : "${UP_SPECIALARGS:=true}"
105 local ret=0
106 # echo "$UP_TODIRECTORY" "$UP_SPECIALARGS"
107 if "$UP_SPECIALARGS"; then
108 case "$1" in
109 '') cd "$UP_TODIRECTORY" ;;
110 up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;;
111 --) UP_SPECIALARGS=false up "${@:2}" ;;
112 -*) if (( "$1" == -1 )); then
113 up
114 else
115 UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 ))
116 fi
117 ;;
118 *) while cd ..; do
119 case "$PWD" in
120 /) ret=1; break ;;
121 */"$1") break ;;
122 esac
123 done
124 ;;
125 esac
126 else
127 case "$1" in
128 '') cd "$UP_TODIRECTORY" ;;
129 *) while cd ..; do
130 case "$PWD" in
131 /) ret=1; break ;;
132 */"$1") break ;;
133 esac
134 done
135 ;;
136 esac
137 fi
138 UP_TODIRECTORY=
139 UP_SPECIALARGS=
140 return "$ret"
89} 141}