-
Notifications
You must be signed in to change notification settings - Fork 0
/
_bash_funs
executable file
·751 lines (673 loc) · 18.3 KB
/
_bash_funs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# -*- shell-script -*-
case "x$TERM" in
xxterm*|xscreen)
# probably can support title bar setting with ansi codes
_settitle () {
echo -en "\033]0;$*\a"
}
;;
*)
# on some terminals, such as bad vt100 clones, this can cause
# crap to pop out all over the screen. on real vt100 terminals
# and others that have a hardware status line, this updates the
# status bar on the bottom line. hopefully, termlib is in good
# shape for the terminal we are using.
if tput tsl >/dev/null 2>&1; then
# definitely supports title bar/status line setting... and we have tput
_settitle () {
tput tsl; echo -n "$*"; tput fsl
}
else
# we can't set the title, :-(
_settitle () {
:
}
fi
;;
esac
# override automatic titlebar setting with a fixed string
settitle () {
_tb_override="$*"
_settitle "$*"
}
# old csh commands
setenv () {
export $1="$2"
}
unsetenv () {
unset $*
}
# quiet type - just return the exit code
qtype () {
type "$@" >/dev/null 2>&1
}
# recursive ls piped through $PAGER
lr () {
ls -alR $* | $PAGER
}
# annoyingly, some versions of dc don't support everything
# that this command requires. get used to using jot on those.
range () {
case "x$2" in
x)
start=1
end=$1
incr=1
;;
x[0-9]*)
start=$1
end=$2
case "x$3" in
x)
incr=1
;;
x[0-9]*)
incr=$3
;;
esac
esac
usage=0
case "x$start" in
x*[^0-9]*|x)
usage=1
;;
esac
case "x$end" in
x*[^0-9]*|x)
usage=1
;;
esac
case "x$incr" in
x*[^0-9]*|x)
usage=1
;;
esac
if [ $usage -eq 1 ]; then
echo "args must be numeric"
echo "usage: range [ a [ b { c } ] ]"
echo " if one argument, arg is end, start and incr is 1."
echo " if two arguments, args are start and end, incr is 1."
echo " if three arguments, args are start, end, and incr."
return -1
fi
if qtype jot; then
jot - $start $end $incr
elif qtype dc; then
echo '[li p' $incr '+ d si' $end '!<a] sa' $start 'silax' | dc
elif qtype perl; then
# no dc or jot? sheesh, let's just fake it with perl
perl -e '$start = shift; $end = shift; $incr = shift;
for ($i = $start; $i <= $end; $i += $incr) {
print $i, "\n";
}' $start $end $incr
else
# no perl, either? gasp!
echo "sorry, man, I tried. you're on your own. here, let me"
echo "get you started... 1 2 3 4"
return -1
fi
}
# csh used to have this thing called "which" that would tell you
# which thing in your path was going to get run when you typed a
# non-qualified command in. unfortunately, the csh version ran
# in a subshell and could get it wrong if you had locally modified
# your path in some way. this shell function uses the shell builtin
# "type" to do the same thing, but it's more accurate since it runs
# right in the same shell you are using.
which () {
local doall=
local shellin=
local usage="usage: which [ -a ] [ -s ] prog [ prog ... ]
-a := show all matches
-s := show path only"
if [ "$1" = "" ]; then
echo $usage
return -1
fi
if [ "$1" = "-a" -o "$1" = "-s" ]; then
if [ "$1" = "-a" ]; then
doall=-all
else
shellin=-path
fi
shift
fi
if [ "$1" = "-a" -o "$1" = "-s" ]; then
if [ "$1" = "-a" ]; then
doall=-all
else
shellin=-path
fi
shift
fi
if [ "$1" = "" ]; then
echo $usage
return
fi
while [ "$1" != "" ]; do
if [ "$1" != "`basename $1`" ]; then
echo "which: do not specify a path"
return
fi
type $doall $shellin $1
shift
done
}
# some simple functions to handle remembering choice tidbits
# in a plain text file. I use this thing all the time. I
# used to have MEMORY_FILE be $HOME/.memory, but I've since
# moved it into my Drobox, which is very handy as it automatically
# stays up-to-date on multiple machines now.
memorize () {
if [ -f "$MEMORY_FILE" ]; then
echo "-- " >> "$MEMORY_FILE"
fi
case "x$1" in
x)
echo "enter text to be memorized"
cat - >> "$MEMORY_FILE"
echo "text has been memorized"
;;
*)
echo "$@" >> "$MEMORY_FILE"
echo "arguments have been memorized"
;;
esac
}
remember () {
if [ -f "$MEMORY_FILE" ]; then
case "x$1" in
x)
less -i "$MEMORY_FILE"
;;
*)
grep -i "$1" "$MEMORY_FILE" | less -i
;;
esac
fi
}
memory () {
$EDITOR "$MEMORY_FILE"
}
## functions for bluehost/EIG
k () {
ssh keyserv.unifiedlayer.com "$1"
}
## functions for Verisys
deploy () {
if [ -d .git ]; then
if git branch --list -a | grep -sq remotes/origin/deploy; then
local orig_branch
local project
# NOTE: this depends on the remote url looking something like
# git@github.com:verisys/<project>.git
project=`git config remote.origin.url |
sed -e 's/^git\@github.com:verisys\///; s/\.git$//'`
orig_branch=`git symbolic-ref --short HEAD`
git checkout master
git pull && git checkout deploy && git merge --ff-only master && git push
git checkout "$orig_branch"
Mail -s "please deploy $project" itsupport@verisys.com <<EOL
Code has been merged to the deploy branch of $project. Please
activate the deployment process at your earliest convenience.
Thank you.
EOL
else
echo "there is no remote 'deploy' branch in this repo. it is"
echo "unlikely that we will be able to deploy it. sorry."
fi
else
echo "this doesn't look like a repo..."
echo "please run deploy from within a repo working dir"
fi
}
# from https://github.com/paoloantinori/hhighlighter
h() {
_usage() {
echo "usage: YOUR_COMMAND | h [-idn] args...
-i : ignore case
-d : disable regexp
-n : invert colors"
}
local _OPTS
# detect pipe or tty
if [[ -t 0 ]]; then
_usage
return
fi
# manage flags
while getopts ":idnQ" opt; do
case $opt in
i) _OPTS+=" -i " ;;
d) _OPTS+=" -Q " ;;
n) n_flag=true ;;
Q) _OPTS+=" -Q " ;;
# let's keep hidden compatibility with -Q for original ack users
\?) _usage
return ;;
esac
done
shift $(($OPTIND - 1))
# set zsh compatibility
[[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays && setopt ignorebraces
local _i=0
if [[ -n $H_COLORS_FG ]]; then
local _CSV="$H_COLORS_FG"
local OLD_IFS="$IFS"
IFS=','
local _COLORS_FG=()
for entry in $_CSV; do
_COLORS_FG=("${_COLORS_FG[@]}" "$entry")
done
IFS="$OLD_IFS"
else
_COLORS_FG=(
"underline bold red" \
"underline bold green" \
"underline bold yellow" \
"underline bold blue" \
"underline bold magenta" \
"underline bold cyan"
)
fi
if [[ -n $H_COLORS_BG ]]; then
local _CSV="$H_COLORS_BG"
local OLD_IFS="$IFS"
IFS=','
local _COLORS_BG=()
for entry in $_CSV; do
_COLORS_BG=("${_COLORS_BG[@]}" "$entry")
done
IFS="$OLD_IFS"
else
_COLORS_BG=(
"bold on_red" \
"bold on_green" \
"bold black on_yellow" \
"bold on_blue" \
"bold on_magenta" \
"bold on_cyan" \
"bold black on_white"
)
fi
if [[ -z $n_flag ]]; then
#inverted-colors-last scheme
_COLORS=("${_COLORS_FG[@]}" "${_COLORS_BG[@]}")
else
#inverted-colors-first scheme
_COLORS=("${_COLORS_BG[@]}" "${_COLORS_FG[@]}")
fi
if [[ "$#" -gt ${#_COLORS[@]} ]]; then
echo "You have passed to hhighlighter more keywords to search than the number of configured colors.
Check the content of your H_COLORS_FG and H_COLORS_BG environment variables or unset them to use default 12 defined colors."
return 1
fi
if [ -n "$ZSH_VERSION" ]; then
local WHICH="whence"
else [ -n "$BASH_VERSION" ]
local WHICH="type -P"
fi
if ! ACKGREP_LOC="$($WHICH ack-grep)" || [ -z "$ACKGREP_LOC" ]; then
if ! ACK_LOC="$($WHICH ack)" || [ -z "$ACK_LOC" ]; then
echo "ERROR: Could not find the ack or ack-grep commands"
return 1
else
local ACK=$($WHICH ack)
fi
else
local ACK=$($WHICH ack-grep)
fi
# build the filtering command
for keyword in "$@"
do
local _COMMAND=$_COMMAND"$ACK $_OPTS --noenv --flush --passthru --color --color-match=\"${_COLORS[$_i]}\" '$keyword' |"
_i=$_i+1
done
#trim ending pipe
_COMMAND=${_COMMAND%?}
#echo "$_COMMAND"
cat - | eval $_COMMAND
}
# from quora:
# http://www.quora.com/What-are-some-of-the-best-command-prompt-or-terminal-tricks
extract () {
if [ -f "$1" ] ; then
case $1 in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# this doesn't appear to work any more... maybe weatherunderground
# is requiring a key of some kind for their api access. It appears
# to work from my browser, but not from curl.
weather() {
wu="http://api.wunderground.com/auto/wui/geo/ForcastXML/index.xml"
curl -s "$wu?query=${@:-84123}" |
gunzip |
perl -ne 's/&deg;/°/g;
/<title>([^<]+)/ && printf "%s: ", $1;
/<fcttext>([^<]+)/ && print $1, "\n"'
}
define() {
curl -s "http://www.collinsdictionary.com/dictionary/english/$*" |
sed -n '/class="def"/p' |
awk '{gsub(/.*<span class="def">|<\/span>.*/,"");print}' |
sed "s/<[^>]\+>//g" |
par
}
epoch() {
TESTREG="[\d{10}]"
if [[ "$1" =~ $TESTREG ]]; then
# is epoch
date -d @$*
else
# is date
if [ $# -gt 0 ]; then
date +%s --date="$*"
else
date +%s
fi
fi
}
# named kill, yeah, some platforms have "killall" that does this, but on
# some other platforms (I'm looking at you, Solaris), killall will shut
# the system down for you (if you're root). probably don't want to do that.
# this script tries to figure out if you've got pkill or a sane killall
# and falls back to grepping the output of ps if all else fails.
nkill () {
usage="usage: nkill [ -u uid ] [ -s signal ] pattern"
# defaults
uid=`id -u`
sig=TERM
# process args
while [ "$1" ]; do
case "x$1" in
x-u)
uid="$2"
shift; shift
;;
x-s)
sig="$2"
shift; shift;
;;
x)
echo "no pattern passed"
echo $usage
return
;;
x*)
pattern="$1"
shift
;;
esac
done
# sanity check args
case "x$uid" in
x)
echo "-u option specified but no uid given"
echo $usage
return
;;
x*[^0-9]*)
echo "uid for -u option must be numeric"
echo $usage
return
;;
esac
case "x$sig" in
xSIG*)
:
;;
x[0-9]*)
:
;;
*)
sig="SIG$sig"
;;
esac
if kill -l | grep -sqw "$sig"; then
:
else
echo "unknown signal, select from one of these"
kill -l
echo $usage
return
fi
if qtype pkill; then
pkill -"$sig" -U "$uid" "$pattern"
elif qtype killall; then
killall -u "$uid" -"$sig" "$pattern"
else
ps -efu | awk '/$pattern/ {print $1, $2}' | awk '/^$uid / {print $2}' |
while read pid; do
kill -"$sig" $pid
done
fi
}
# try to figure out if we're in a mosh session
_in_mosh_session () {
mypid=$$
while [ "$mypid" -ne 1 ]; do
pso=`ps l$mypid | sed -e 1d`
case "$MACH_TYPE" in
linux)
ppid=`echo $pso | awk '{print $4}'`
mypid=`echo $pso | awk '{print $3}'`
;;
darwin)
ppid=`echo $pso | awk '{print $3}'`
mypid=`echo $pso | awk '{print $2}'`
;;
*)
# just assume we're not
return 1
;;
esac
if echo $pso | grep -sq mosh-server; then
# yep, we're in a mosh session
return 0
fi
mypid=$ppid
done
# nope, we made it up the process tree to init without seeing mosh,
# so we're not in a mosh sessionn
return 1
}
# based on http://brettterpstra.com/2015/02/20/shell-trick-printf-rules/
# don't know if I'll use them, but they're pretty cool. I've tweaked them
# up a bit to use the smacs and rmacs sequences if available. Note that on
# some terminals (notably "screen") you have to send enacs once first, before
# smacs and rmacs will work as expected.
_rule () {
if tput cols >/dev/null 2>&1; then
cols=$(( $(tput cols) - 2 ))
else
# try to get columns from terminal
if stty -a | grep -sq columns; then
cols=$(stty -a | grep columns | sed -e 's/^.*columns //' -e 's/;$//')
else
# try to get columns from environment
if [ "$COLUMNS" ]; then
cols=$COLUMNS
else
# punt?
cols=80
fi
fi
cols=$(( $cols - 2 ))
fi
if [ "$1" ]; then
# user specified a line drawing character of their choice, use it
_lc="$1"
else
# default to '-'
_lc="-"
# unless the terminal supports smacs, in which case we will use
# the magic line drawing characters in the alternate character set
if tput smacs >/dev/null 2>&1; then
case "$TERM" in
screen)
# screen and tmux require sending enacs before smacs and rmacs
# will work, so do that now (originally was going to just send
# it once at the beginning of the terminal session, but if anyone
# (I'm looking at you, "reset(1)") resets the terminal, you'll
# have to send it again. since it doesn't hurt to send it, just
# do it here.
if tput enacs >/dev/null 2>&1; then
tput enacs
fi
;;
esac
tput smacs
# "q" is the magical horizontal line character
_lc="q"
# unless we're in a mosh session, because mosh eats our nice smacs
# and rmacs codes for breakfast. we have to defer down to using
# the unicode line drawing character in this case, which I hate
# doing for portability reasons, but see no alternative. at least
# we can confirm that we have a UTF-8 character set first.
if _in_mosh_session; then
case "$LANG" in
*UTF-8*)
_lc="\0342\0224\0200"
;;
esac
fi
fi
fi
printf -v _hr "%*s" $cols && echo -ne ${_hr// /${_lc}}
if tput rmacs >/dev/null 2>&1; then
tput rmacs
fi
}
rule () {
echo -e $(_rule "$1")
}
rulem () {
if [ $# -eq 0 ]; then
echo "usage: rulem MESSAGE [RULE_CHARACTER]"
return 1
fi
echo -n $(_rule "$2")
tput hpa 2 && echo "$1"
}
# generate my prompt string... this is evolving
prompt_update () {
case "$interactive" in
true)
:
;;
*)
# don't be setting the prompt on a non-interactive shell
return
;;
esac
# update the history... this keeps a common history between
# all shells on the same machine. this takes some getting used
# to unless you're familiar with ksh that does this by default.
history -a; history -c; history -r #nkill -s USR1 bash
# virtual env active?
if [ "$VIRTUAL_ENV" ]; then
venv="(`basename \"$VIRTUAL_ENV\"`) "
else
venv=""
fi
# set the title bar/status line if we can
if [ -z "$_tb_override" ]; then
_ts="$(id -nu)@$(hostname -s):$(pwd)"
_ts="$_ts ($(tty|sed -e 's,^/dev/tty,,'))"
qtype _settitle && _settitle "$_ts"
fi
# draw a horizontal line between commands?
hrule=$(_rule)
hpa2=$(tput hpa 2)
# pay no attention to the millions of backslashes to follow...
# default in case __git_ps1 isn't there
PS1="${hrule}\r${hpa2} ${venv}\u@\h:\w \n\! bash\\\$ "
# use __get_ps1 to set up the PS1 if available
qtype __git_ps1 && \
__git_ps1 "${hrule}\r${hpa2} ${venv}\u@\h:\w" " \n\! bash\\\$ "
}
# from Aaron Toppence (eightyeight) https://ae7.st/p/734
expandurl () {
(torsocks wget --spider -O - -S $1 2>&1 |
awk '/^Location/ {gsub("\\?utm_.*$",""); print $2; exit 0}
/socks5 libc connect: Connection refused/ {exit 1}') ||
(echo "warning, TOR not enabled"
echo -n "okay to connect directly? (y/N) "
read resp
case "x$resp" in
x[yY]*)
wget --spider -O - -S $1 2>&1 |
awk '/^Location/ {gsub("\\?utm_.*$",""); print $2; exit 0}'
;;
esac)
}
# some OS specific utilities
case $MACH_TYPE in
macosx)
eject () {
case "x$1" in
x)
disk=`osascript -e 'tell application "Finder" to get name of every disk whose ejectable is true' 2>/dev/null |
perl -ne 'print((split(", "))[0], "\n");'`
;;
*)
disk="$1"
;;
esac
if [ "$disk" ]; then
echo "ejecting $disk"
osascript -e "tell application \"Finder\" to eject \"$disk\""
else
echo "no ejectable media"
fi
}
ejectall () {
osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'
}
# completion helper
_eject () {
local cur=${COMP_WORDS[COMP_CWORD]}
local IFS=$'\n'
local escaped_single_quote="'\''"
local i=0
COMPREPLY=( $( cd /Volumes; compgen -o dirnames -- "$cur" ) )
for entry in ${COMPREPLY[*]}; do
if [[ "${cur:0:1}" == "'" ]]; then
# started with single quote, escaping only other single quotes
# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
COMPREPLY[$i]="${entry//\'/${escaped_single_quote}}"
elif [[ "${cur:0:1}" == "\"" ]]; then
# started with double quote, escaping all double quotes and all backslashes
# ["]bla'bla"bla\bla bla -> ["]bla'bla\"bla\\bla bla
entry="${entry//\\/\\\\}"
COMPREPLY[$i]="${entry//\"/\\\"}"
else
# no quotes in front, escaping _everything_
# [ ]bla'bla"bla\bla bla -> [ ]bla\'bla\"bla\\bla\ blal
entry="${entry//\\/\\\\}"
entry="${entry//\'/\'}"
entry="${entry//\"/\\\"}"
COMPREPLY[$i]="${entry// /\\ }"
fi
(( i++ ))
done
return 0
}
complete -F _eject eject
;;
esac