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.bash81
1 files changed, 49 insertions, 32 deletions
diff --git a/bash/functions.bash b/bash/functions.bash index 98872f2..c5bcddb 100644 --- a/bash/functions.bash +++ b/bash/functions.bash
@@ -1,45 +1,62 @@
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"; shift 6 local e needle="$1"
7 for e; do 7 shift
8 [[ "$e" == "$needle" ]] && { 8 for e; do
9 return 0 9 [[ "$e" == "$needle" ]] && {
10 } 10 return 0
11 done 11 }
12 return 1 12 done
13 return 1
13} 14}
14 15
15rebashrc() { # rebashrc 16rebashrc() { # rebashrc
16 ## Reload ~/.bashrc 17 ## Reload ~/.bashrc
17 printf "Loading ~/.bashrc..." >&2 18 printf "Loading ~/.bashrc..." >&2
18 if source "$HOME/.bashrc"; then 19 if source "$HOME/.bashrc"; then
19 echo "OK." >&2 20 echo "OK." >&2
20 else 21 else
21 echo "ERROR!" >&2 22 echo "ERROR!" >&2
22 fi 23 fi
23} 24}
24 25
25first_which() { # first_which COMMAND... 26first_which() { # first_which COMMAND...
26 ## 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.
27 while :; do 28 while :; do
28 command -v "$1" && break 29 command -v "$1" && break
29 [ -z "$1" ] && return 1 30 [ -z "$1" ] && return 1
30 shift 31 shift
31 done 32 done
32} 33}
33 34
34please() { # please [COMMAND...] 35please() { # please [COMMAND...]
35 # 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 !!)
36 # if run WITH arguments, alias as sudo 37 # if run WITH arguments, alias as sudo
37 history -d -1 38 history -d -1
38 if [ -z "$1" ]; then 39 if [ -z "$1" ]; then
39 #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q') 40 #set -- $(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q')
40 set -- $(fc -lnr | sed 1q) 41 set -- $(fc -lnr | sed 1q)
41 fi 42 fi
42 echo >&2 sudo "$@" 43 echo >&2 sudo "$@"
43 history -s sudo "$@" 44 history -s sudo "$@"
44 "${DEBUG:-false}" || sudo "$@" 45 "${DEBUG:-false}" || sudo "$@"
45} 46}
47
48mkcd() {
49 if [ $# -lt 1 ]; then
50 command cd
51 return
52 fi
53 if ! [ -d "$1" ]; then
54 read -p "$1 doesn't exist. Create (Y/n)? " yn
55 case "$yn" in
56 n* | N*) return 1 ;;
57 *) mkdir -p "$1" ;;
58 esac
59 fi
60 command cd "$1"
61}
62alias cd='mkcd '