forked from isaacs/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.extra.bashrc
executable file
·868 lines (798 loc) · 21.6 KB
/
.extra.bashrc
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
######
# .extra.bashrc - Isaac's Bash Extras
# This file is designed to be a drop-in for any machine that I log into.
# Currently, that means it has to work under Darwin, Ubuntu, and yRHEL
#
# Per-platform includes at the bottom, but most functionality is included
# in this file, and forked based on resource availability.
#
# Functions are preferred over shell scripts, because then there's just
# a few files to rsync over to a new host for me to use it comfortably.
#
# .extra_Darwin.bashrc has significantly more stuff, since my mac is also
# a GUI environment, and my primary platform.
######
# Note for Leopard Users #
# If you use this, it will probably make your $PATH variable pretty long,
# which will cause terrible performance in a stock Leopard install.
# To fix this, comment out the following lines in your /etc/profile file:
# if [ -x /usr/libexec/path_helper ]; then
# eval `/usr/libexec/path_helper -s`
# fi
# Thanks to "allan" in irc://irc.freenode.net/#textmate for knowing this!
echo "loading bash extras..."
if [ "$BASH_COMPLETION_DIR" == "" ]; then
[ -f /opt/local/etc/bash_completion ] && . /opt/local/etc/bash_completion
[ -f /etc/bash_completion ] && . /etc/bash_completion
fi
# set some globals
if ! [ -f "$HOME" ]; then
export HOME="$(echo ~)"
fi
# try to avoid polluting the global namespace with lots of garbage.
# the *right* way to do this is to have everything inside functions,
# and use the "local" keyword. But that would take some work to
# reorganize all my old messes. So this is what I've got for now.
__garbage_list=""
__garbage () {
local i
if [ $# -eq 0 ]; then
for i in ${__garbage_list}; do
unset $i
done
unset __garbage_list
else
for i in "$@"; do
__garbage_list="${__garbage_list} $i"
done
fi
}
__garbage __set_path
__set_path () {
local var="$1"
local path="$2"
! [ -d ~/bin ] && mkdir ~/bin
local path_elements="${path//:/ }"
path=""
local i
for i in $path_elements; do
[ -d $i ] && path="$path$i "
done
export $var=$(path=$(echo $path); echo ${path// /:})
}
__set_path "PATH" "$HOME/bin:$HOME/scripts:/home/y/bin:$HOME/dev/js/narwhal/bin:$HOME/dev/js/jack/bin:/opt/local/sbin:/opt/local/bin:/opt/local/libexec:/opt/local/apache2/bin:/opt/local/lib/mysql/bin:/opt/local/lib/erlang/bin:/usr/local/sbin:/usr/local/bin:/usr/local/libexec:/usr/sbin:/usr/bin:/usr/libexec:/sbin:/bin:/libexec:/usr/X11R6/bin:/home/y/include:/opt/local/share/mysql5/mysql:/usr/local/mysql/bin:/opt/local/include:/opt/local/apache2/include:/usr/local/include:/usr/include:/usr/X11R6/include:/opt/local/etc/LaunchDaemons/org.macports.lighttpd/:$HOME/appsup/TextMate/Support/bin"
__set_path CLASSPATH "./:~/dev/js/rhino/build/classes"
__set_path CDPATH ".:..:$HOME/dev:$HOME"
# Use UTF-8, and throw errors in PHP and Perl if it's not available.
# Note: this is VERY obnoxious if UTF8 is not available!
# That's the point!
export LC_CTYPE=en_US.UTF-8
export LC_ALL=""
export LANG=$LC_CTYPE
export LANGUAGE=$LANG
export TZ=America/Los_Angeles
export HISTSIZE=10000
export HISTFILESIZE=1000000000
# I prefer to use : instead of ^ for history replacements
# much faster to type. It'd be neat to use /, but then it gets
# confused with absolute paths, like "/bin/env"
export histchars="!:#"
__garbage __shopt
__shopt () {
local i
for i in "$@"; do
shopt -s $i
done
}
# see http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html#The-Shopt-Builtin
__shopt \
histappend histverify histreedit \
cdspell expand_aliases cmdhist \
hostcomplete no_empty_cmd_completion nocaseglob
hist () {
history \
| grep "$@" \
| uniq -f 2 -u
}
alias ..="cd .."
alias -- -="cd -"
# read a line from stdin, write to stdout.
getln () { read "$@" t && echo $t; }
# chooses the first argument that matches a file in the path.
choose_first () {
for i in "$@"; do
if ! [ -f "$i" ] && inpath "$i"; then
i="$(which "$i")"
fi
if [ -f "$i" ]; then
echo $i
break
fi
done
}
# fail if the file is not an executable in the path.
inpath () {
! [ $# -eq 1 ] && echo "usage: inpath <file>" && return 1
f="$(which "$1" 2>/dev/null)"
[ -f "$f" ] && return 0
return 1
}
# show a certain line of a file, or stdin
line () {
sed ${1-0}'!d;q' < ${2-/dev/stdin}
}
# headless <command> [<key>]
# to reconnect, do: headless "" <key>
headless () {
if [ "$2" == "" ]; then
hash=$(md5 -qs "$1")
else
hash="$2"
fi
if [ "$1" != "" ]; then
dtach -n /tmp/headless-$hash bash -l -c "$1"
else
dtach -A /tmp/headless-$hash bash -l
fi
}
# do something in the background
back () {
( "$@" ) &
}
# do something very quietly.
quiet () {
( "$@" ) &>/dev/null
}
#do something to all the things on standard input.
# echo 1 2 3 | foreach echo foo is like calling echo foo 1; echo foo 2; echo foo 3;
foreach () {
local $i
for i in $(cat /dev/stdin); do
"$@" $i;
done
}
# test javascript files for syntax errors.
if inpath yuicompressor; then
testjs () {
local i
local err
for i in $(find . -name "*.js"); do
err="$(yuicompressor -o /dev/null $i 2>/dev/stdout)"
if [ "$err" != "" ]; then
echo "$i has errors:"
echo "$err"
fi
done
}
fi
# give a little colou?r to grep commands, if supported
grep=grep
if [ "$(grep --help | grep color)" != "" ]; then
grep="grep --color"
elif [ "$(grep --help | grep colour)" != "" ]; then
grep="grep --colour"
fi
alias grep="$grep"
# substitute "this" for "that" if "this" exists and is in the path.
substitute () {
! [ $# -eq 2 ] && echo "usage: substitute <desired> <orig>" && return 1
inpath "$1" && new="$(which "$1")" && alias $2="$new"
}
substitute yssh ssh
substitute yscp scp
export SVN_RSH=$(choose_first yssh ssh)
export RSYNC_RSH=$(choose_first yssh ssh)
export INPUTRC=$HOME/.inputrc
export POSIXLY_CORRECT=1
__garbage has_yinst
has_yinst=0
inpath yinst && has_yinst=1
# useful commands:
__get_edit_cmd () {
echo "$( choose_first "$@" )"
}
# my list of editors, by preference.
__edit_cmd="$( __get_edit_cmd mate vim vi pico ed )"
alias edit="${__edit_cmd}"
alias sued="sudo ${__edit_cmd}"
export EDITOR="$( choose_first ${__edit_cmd}_wait ${__edit_cmd} )"
export VISUAL="$EDITOR"
__garbage __get_edit_cmd __edit_cmd
# shebang <file> <program> [<args>]
shebang () {
local sb="shebang"
if [ $# -lt 2 ]; then
echo "usage: $sb <file> <program> [<arg string>]"
return 1
elif ! [ -f "$1" ]; then
echo "$sb: $1 is not a file."
return 1
fi
if ! [ -w "$1" ]; then
echo "$sb: $1 is not writable."
return 1
fi
local prog="$2"
! [ -f "$prog" ] && prog="$(which "$prog" 2>/dev/null)"
if ! [ -x "$prog" ]; then
echo "$sb: $2 is not executable, or not in path."
return 1
fi
chmod ogu+x "$1"
prog="#!$prog"
[ "$3" != "" ] && prog="$prog $3"
if ! [ "$(head -n 1 "$1")" == "$prog" ]; then
local tmp=$(mktemp shebang.XXXX)
( echo $prog; cat $1 ) > $tmp && cat $tmp > $1 && rm $tmp && return 0 || \
echo "Something fishy happened!" && return 1
fi
return 0
}
# Probably a better way to do this, but whatevs.
rand () {
echo $(php -r 'echo mt_rand();')
}
pickrand () {
local cnt=0
local tst="-d"
if [ $# == 1 ]; then
tst="$1"
fi
for i in *; do
[ $tst "$i" ] && let 'cnt += 1'
done
[ $cnt -eq 0 ] && return 1
local r=$(rand)
local p=0
let 'p = r % cnt'
# echo "[$cnt $r --- $p]"
cnt=0
for i in *; do
# echo "[$cnt]"
[ $tst "$i" ] && let 'cnt += 1' && [ $cnt -eq $p ] && echo "$i" && return
done
}
# md5 from the command line
# I like the BSD/Darwin "md5" util a bit better than md5sum flavor.
# Ported here to always have it.
# Yeah, that's right. My bash profile has a PHP program embedded
# inside. You wanna fight about it?
if ! inpath md5 && inpath php; then
# careful on this next trick. The php code can *not* use single-quotes.
echo '<?php
// The BSD md5 checksum program, ported to PHP by Isaac Z. Schlueter
exit main($argc, $argv);
function /* int */ main ($argc, $argv) {
global $bin;
$return = true;
$bin = basename( array_shift($argv) );
$return = 0;
foreach (parseargs($argv, $argc) as $target => $action) {
// echo "$action($target)\n";
if ( !$action( $target ) ) {
$return ++;
}
}
// convert to bash success/failure flag
return $return;
}
function parseargs ($argv, $argc) {
$actions = array();
$getstring = false;
$needstdin = true;
foreach ($argv as $arg) {
// echo "arg: $arg\n";
if ($getstring) {
$getstring = false;
$actions[ "\"$arg\"" ] = "cksumString";
continue;
}
if ($arg[0] !== "-") {
// echo "setting $arg to cksumFile\n";
$needstdin = false;
$actions[$arg] = "cksumFile";
} else {
// is a flag
$arg = substr($arg, 1);
if (strlen($arg) === 0) {
$actions["-"] = "cksumFile";
} else {
while (strlen($arg)) {
$flag = $arg{0};
$arg = substr($arg, 1);
switch ($flag) {
case "s":
if ($arg) {
$actions["\"$arg\""] = "cksumString";
$arg = "";
} else {
$getstring = true;
}
$needstdin = false;
break;
case "p": $actions[] = "cksumStdinPrint"; $needstdin = false; break;
case "q": flag("quiet", true); break;
case "r": flag("reverse",true); break;
case "t": $actions["timeTrial"] = "timeTrial"; $needstdin = false; break;
case "x": $actions["runTests"] = "runTests"; $needstdin = false; break;
default : $actions["$flag"] = "usage"; $needstdin = false; break;
} // switch
} // while
} // strlen($arg)
}
} // end foreach
if ($getstring) {
global $bin;
// exited without getting a string!
error_log("$bin: option requires an argument -- s");
usage();
}
if ($needstdin) {
$actions[] = "cksumStdin";
}
return $actions;
}
/*
-s string
Print a checksum of the given string.
-p
Echo stdin to stdout and appends the MD5 sum to stdout.
-q
Quiet mode - only the MD5 sum is printed out. Overrides the -r option.
-r
Reverses the format of the output. This helps with visual diffs.
Does nothing when combined with the -ptx options.
-t
Run a built-in time trial.
-x
Run a built-in test script.
*/
function cksumFile ($file) {
$missing = !file_exists($file);
$isdir = $missing ? 0 : is_dir($file); // only call if necessary
if ( $missing || $isdir ) {
global $bin;
error_log("$bin: $file: " . ($missing ? "No such file or directory" : "is a directory."));
// echo "bout to return\n";
return false;
}
output("MD5 (%s) = %s", $file, md5(file_get_contents($file)));
}
function cksumStdin () {
$stdin = file_get_contents("php://stdin");
writeln(md5($stdin));
return true;
}
function cksumStdinPrint () {
$stdin = file_get_contents("php://stdin");
output("%s%s", $stdin, md5($stdin), array("reverse"=>false));
return true;
}
function cksumString ($str) {
return output("MD5 (%s) = %s", $str, md5(substr($str,1,-1)));
}
function runTests () {
writeln("MD5 test suite:");
$return = true;
foreach (array(
"", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890") as $str ) {
$return = $return && cksumString("\"$str\"");
}
return $return;
}
function timeTrial () {
error_log("Time trial not supported in this version.");
return false;
}
function flag ($flag, $set = null) {
static $flags = array();
$f = in_array($flag, $flags) ? $flags[$flag] : ($flags[$flag] = false);
return ($set === null) ? $f : (($flags[$flag] = (bool)$set) || true) && $f;
}
function usage ($option = "") {
global $bin;
if (!empty($option)) {
error_log("$bin: illegal option -- $option");
}
writeln("usage: $bin [-pqrtx] [-s string] [files ...]");
return false;
}
function output ($format, $input, $digest, $flags = array()) {
$orig_flags = array();
foreach ($flags as $flag => $value) {
$orig_flags[$flag] = flag($flag);
flag($flag, $value);
}
if ( flag("quiet") ) {
writeln($digest);
} elseif ( flag("reverse") ) {
writeln( "$digest $input" );
} else {
writeln( sprintf($format, $input, $digest) );
}
foreach ($orig_flags as $flag=>$value) {
flag($flag, $value);
}
return true;
}
function writeln ($str) {
echo "$str\n";
}
?>'>~/bin/md5
shebang ~/bin/md5 php "-d open_basedir="
fi
# a friendlier delete on the command line
! [ -d ~/.Trash ] && mkdir ~/.Trash
chmod 700 ~/.Trash
alias emptytrash="find ~/.Trash -not -path ~/.Trash -exec rm -rf {} \; 2>/dev/null"
if ! inpath del; then
if [ -d ~/.Trash ]; then
del () {
for i in "$@"; do
mv "$i" ~/.Trash/
done
}
else
alias del=rm
fi
fi
alias mvsafe="mv -i"
lscolor=""
__garbage lscolor
if [ "$TERM" != "dumb" ] && [ -f "$(which dircolors 2>/dev/null)" ]; then
eval "$(dircolors -b)"
lscolor=" --color=auto"
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
fi
ls_cmd="ls$lscolor"
__garbage ls_cmd
alias ls="$ls_cmd"
alias la="$ls_cmd -laF"
alias lal="$ls_cmd -laFL"
alias ll="$ls_cmd -lF"
alias ag="alias | $grep"
fn () {
local func=$(declare -f "$1")
[ -z "$func" ] && echo "$1 is not a function" > /dev/stderr && return 1
echo $func && return 0
}
alias lg="$ls_cmd -laF | $grep"
alias chdir="cd"
# alias more="less -e"
export MANPAGER=more
alias lsdevs="sudo lsof | $grep ' /dev'"
# domain sniffing
wi () {
whois $1 | egrep -i '(registrar:|no match|record expires on|holder:)'
}
#make tree a little cooler looking.
alias tree="tree -CAFa -I 'rhel.*.*.package|.git' --dirsfirst"
__garbage yapr
if [ $has_yinst -eq 1 ]; then
yapr="yinst restart yapache"
elif inpath lighttpd.wrapper; then
yapr="sudo lighttpd.wrapper restart"
elif [ -f /etc/init.d/lighttpd ]; then
yapr="sudo /etc/init.d/lighttpd reload"
elif inpath apache2ctl; then
yapr="sudo apache2ctl graceful"
elif inpath apachectl; then
yapr="sudo apachectl graceful"
else
# very strange!
yapr="echo Looks like lighttpd and apache are not installed."
fi
alias yapr="$yapr"
__garbage error_log
error_log="$(choose_first /opt/local/var/log/lighttpd/error.log /home/y/logs/yapache/php-error /home/y/logs/yapache/error /home/y/logs/yapache/error_log /home/y/logs/yapache/us/error_log /home/y/logs/yapache/us/error /opt/local/apache2/logs/error_log /var/log/httpd/error_log /var/log/httpd/error)"
yapl="tail -f $error_log | egrep -v '^E|udbClient'"
alias yaprl="$yapr;$yapl"
alias yapl="$yapl"
prof () {
. ~/.extra.bashrc
}
editprof () {
s=""
if [ "$1" != "" ]; then
s="_$1"
fi
$EDITOR ~/.extra$s.bashrc
prof
}
pushprof () {
[ "$1" == "" ] && echo "no hostname provided" && return 1
local failures=0
local rsync="rsync --copy-links -v -a -z"
for each in "$@"; do
if [ "$each" != "" ]; then
if $rsync ~/.{inputrc,tarsnaprc,profile,extra,git}* $each:~ && \
$rsync ~/.ssh/*{.pub,authorized_keys,config} $each:~/.ssh/; then
echo "Pushed bash extras and public keys to $each"
else
echo "Failed to push to $each"
let 'failures += 1'
fi
fi
done
return $failures
}
if [ $has_yinst -eq 1 ]; then
alias inst="yinst install"
alias yl="yinst ls"
yg () {
yinst ls | $grep "$@"
}
ysg () {
yinst set | $grep "$@"
}
elif inpath port; then
alias inst="sudo port install"
alias yl="port list installed"
yg () {
port list installed | grep "$@"
}
alias upup="sudo port sync && sudo port upgrade outdated"
cleanpkg () {
for i in "$@"; do
sudo port uninstall -f $i
sudo port clean $i
sudo port install $i
done
}
elif inpath apt-get; then
alias inst="sudo apt-get install"
alias yl="dpkg --list | egrep '^ii'"
yg () {
dpkg --list | egrep '^ii' | $grep "$@"
}
alias upup="sudo apt-get update && sudo apt-get upgrade"
fi
alias gci="git commit"
alias gpu="git pull"
ghadd () {
local me="$(git config --get github.user)"
[ "$me" == "" ] && echo "Please enter your github name as the github.user git config." && return 1
# like: "git@github.com:$me/$repo.git"
local mine="$( git config --get remote.origin.url )"
local repo="${mine/git@github.com:$me\//}"
local nick="$1"
local who="$2"
[ "$who" == "" ] && who="$nick"
[ "$who" == "" ] && echo "Whose repo do you want to add?" && return 1
# eg: git://github.com/isaacs/jack.git
local theirs="git://github.com/$who/$repo"
git remote add "$nick" "$theirs"
}
yup () { ypu; }
ypu () {
for i in build upstream; do
git fetch -v $i
done
}
alias gps="git push --all"
gpm () {
git pull $1 master
}
# look up a word
dict () {
curl -s dict://dict.org/d:$1 | perl -ne 's/\r//; last if /^\.$/; print if /^151/../^250/' | more
}
#get the ip address of a host easily.
getip () {
for each in "$@"; do
echo $each
echo "nslookup:"
nslookup $each | grep Address: | grep -v '#' | egrep -o '([0-9]+\.){3}[0-9]+'
echo "ping:"
ping -c1 -t1 $each | egrep -o '([0-9]+\.){3}[0-9]+' | head -n1
done
}
# Show the IP addresses of this machine, with each interface that the address is on.
ips () {
local interface=""
local types='vmnet|en|eth'
local i
for i in $(
ifconfig \
| egrep -o '(^('$types')[0-9]|inet (addr:)?([0-9]+\.){3}[0-9]+)' \
| egrep -o '(^('$types')[0-9]|([0-9]+\.){3}[0-9]+)' \
| grep -v 127.0.0.1
); do
if ! [ "$( echo $i | perl -pi -e 's/([0-9]+\.){3}[0-9]+//g' )" == "" ]; then
interface="$i":
else
echo $interface $i
fi
done
}
# Like the ips function, but for mac addrs.
macs () {
local interface=""
local i
local types='vmnet|en|eth'
for i in $(
ifconfig \
| egrep -o '(^('$types')[0-9]:|ether ([0-9a-f]{2}:){5}[0-9a-f]{2})' \
| egrep -o '(^('$types')[0-9]:|([0-9a-f]{2}:){5}[0-9a-f]{2})'
); do
if [ ${i:(${#i}-1)} == ":" ]; then
interface=$i
else
echo $interface $i
fi
done
}
# set the bash prompt and the title function
! [ "$TITLE" ] && TITLE=''
! [ "${__title}" ] && __title=''
__settitle () {
__title="$1"
if [ "$YROOT_NAME" != "" ]; then
if [ "${__title}" != "" ]; then
TITLE="$YROOT_NAME — ${__title}"
else
TITLE="$YROOT_NAME"
fi
else
TITLE=${__title}
fi
local gittitle=$( __git_ps1 "%s — " 2>/dev/null )
DIR=${PWD/$HOME/\~}
local t=""
[ "$TITLE" != "" ] && t="$TITLE — "
echo -ne "\033]0;$gittitle$t${HOSTNAME_FIRSTPART%%\.*}:$DIR\007"
}
title () {
if [ ${#@} == 1 ]; then
__settitle "$@"
else
echo "$TITLE"
fi
}
#show the short hostname, selected title, and yroot, and update them all on each prompt
export HOSTNAME=$(uname -n);
export HOSTNAME_FIRSTPART=${HOSTNAME%\.yahoo\.com};
__arch=$(uname)
__bg=$([ ${__arch} == "Darwin" ] && echo 44 || echo 42)
__color=$([ ${__arch} == "Darwin" ] && echo 1 || echo 30)
__garbage __arch
export HOSTNAME=$(uname -n)
export HOSTNAME_FIRSTPART=${HOSTNAME%\.yahoo\.com}
PROMPT_COMMAND='history -a
__settitle "${__title}"
echo ""
[ "$TITLE" ] && echo -ne "\033[${__color}m\033[${__bg}m $TITLE \033[0m"
echo -ne "$(__git_ps1 "\033[41m\033[1;37m %s \033[0m")"
echo -ne "\033[40m\033[37m$(whoami)@${HOSTNAME_FIRSTPART}\033[0m:$DIR"'
#this part gets repeated when you tab to see options
PS1="\n[\t \!] \\$ "
# view processes.
alias processes="ps axMuc | egrep '^[a-zA-Z0-9]'"
pg () {
ps aux | $grep "$@" | grep -v "$( echo $grep "$@" )"
}
pid () {
pg "$@" | awk '{print $2}'
}
alias v="ssh visitbread.corp.yahoo.com"
alias vm="ssh visitbread-vm0.corp.yahoo.com"
alias fh="ssh foohack.com"
alias p="ssh isaacs.xen.prgmr.com"
alias st="ssh sistertrain.com"
# shorthand for checking on ssh agents.
sshagents () {
pg -i ssh
set | grep SSH | grep -v grep
find /tmp/ -type s | grep -i ssh
}
# shorthand for creating a new ssh agent.
agent () {
eval $( ssh-agent )
ssh-add
}
vazu () {
rsync -vazuR --stats --no-implied-dirs --delete "$@"
}
repeat () {
local delay
if [ "$2" == "" ]; then
delay=1
else
delay=$2
fi
while sleep $delay; do
clear
date +%s
$1
done
}
# floating-point calculations
calc () {
local expression="$@"
[ "${expression:0:6}" != "scale=" ] && expression="scale=16;$expression"
echo "$expression" | bc
}
# more handy wget for fetching files to a specific filename.
fetch_to () {
local from=$1
local to=$2
[ "$to" == "" ] && to=$( basname "$from" )
[ "$to" == "" ] && echo "usage: fetch_to <url> [<filename>]" && return 1
wget -U "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5" -O "$to" "$from" || return 1
}
# command-line perl prog
alias pie="perl -pi -e "
# c++ compilation shortcuts
stripc () {
local f="$1"
local o="$f"
o=${o%.c}
o=${o%.cpp}
o=${o%.cc}
echo $o
}
cm () {
g++ -o $(stripc "$1") "$1"
}
cr () {
cm "$1" && ./$(stripc "$1")
}
# tarsnap wrappers.
# http://tarsnap.com
ts () {
local e=echo
inpath growlnotify && e="growlnotify -t tarsnap -m "
if [ $# -lt 1 ] || ! [ -e "$1" ]; then
$e "Need to supply a file/directory to back up" > /dev/stderr
return 1
fi
if [ $# -gt 1 ]; then
local errors=0
for i in "$@"; do
ts $i || let 'errors += 1'
done
return $errors
fi
local thetitle=$(title)
local thefile="$1"
$e "backing up $thefile"
title "backing up $thefile"
backupfile="$(hostname):${thefile/\//}:$(date +%Y-%m-%d-%H-%M-%S)"
backupfile=${backupfile//\//-}
tarsnap -cvf "$backupfile" $thefile 2> ~/.tslog
$e "done backing up $thefile"
title "$thetitle"
}
tsbg () {
( ts "$@" ) &
}
tsh () {
# headless <command> [<key>]
headless "ts $@" ts-headless-backup
}
tskill () {
kill -s SIGQUIT $(pid tarsnap)
}
tsabort () {
kill $(pid tarsnap)
}
tslisten () {
tail -f ~/.tslog
}
tsl () {
tslisten
}
#load any per-platform .extra.bashrc files.
__garbage arch machinearch
arch=$(uname -s)
machinearch=$(uname -m)
[ -f ~/.extra_$arch.bashrc ] && . ~/.extra_$arch.bashrc
[ -f ~/.extra_${arch}_${machinearch}.bashrc ] && . ~/.extra_${arch}_${machinearch}.bashrc
[ $has_yinst == 1 ] && [ -f ~/.extra_yinst.bashrc ] && . ~/.extra_yinst.bashrc
inpath "git" && [ -f ~/.git-completion ] && . ~/.git-completion
# call in the cleaner.
__garbage
export BASH_EXTRAS_LOADED=1