3 # redirect output to log file
8 # By default this file is sourced for all ssh commands. This is wonky.
9 # Normally, this file is not sourced when a script is run, and it would be much
10 # better and more consistent if that also happened when when running a script
11 # over ssh. so here we test for conditions of a script under ssh and return if
12 # so. we can override with ssh -t which sets $SSH_TTY, which we can detect
13 # But inside a script, ssh -t won't work, because we aren't using a tty at all.
14 # So we need something else. Command lines and env variables sent across ssh are strictly limited.
15 # We could override an obscure unused LC_var, like telephone, or we could transfer a file.
16 # But I choose to set SendEnv and AcceptEnv ssh vars for BASH_LOGIN_SHELL.
17 # In a private file, i have aliases for if $- == *i*, ssh -t, else ssh.
19 [[ $- != *i* && ! $SSH_CONNECTION ]] && export BASH_LOGIN_SHELL=true
21 if [[ $SSH_CONNECTION ]] \
24 && [[ ! $BASH_LOGIN_SHELL == true ]] \
25 && [[ $- != *i* ]]; then
41 # remove all aliases. aliases provided by the system tend to get in the way,
42 # for example, error happens if I try to define a function the same name as an alias
45 # remove gnome keyring warning messages
46 # there is probably a more proper way, but I didn't find any easily on google
47 unset GNOME_KEYRING_CONTROL
49 #use extra globing features. See man bash, search extglob.
51 #include .files when globbing.
53 #but ignore files name . and ..
54 # this also sets dotglob, but no harm in setting it explicitly
55 export GLOBIGNORE=*/.:*/..
57 # broken with bash_completion package. Saw a bug for this once. Don't anymore.
58 # still broken in wheezy
59 # still buggered in latest stable from the web, version 2.1
60 # perhaps its fixed in newer git version, which fails to make for me
61 # this note is from 6-2014
64 # make tab on an empty line do nothing
65 shopt -s no_empty_cmd_completion
68 # http://bash-completion.alioth.debian.org/
69 # might be sourced by the system already, but I've noticed it not being sourced before
70 if ! type _init_completion &> /dev/null && [[ -r "/usr/share/bash-completion/bash_completion" ]]; then
71 . /usr/share/bash-completion/bash_completion
75 # fix spelling errors for cd, only in interactive shell
77 # append history instead of overwritting it
79 # for compatibility, per gentoo/debian bashrc
81 # attempt to save multiline single commands as single history entries.
87 if [[ $INSIDE_EMACS ]]; then
91 # for readline-complete.el
93 # todo, remote file completion fails, figure out how to turn it off
97 if [[ $- == *i* ]]; then
98 # for readline-complete.el
99 if [[ $INSIDE_EMACS ]]; then
100 bind 'set horizontal-scroll-mode on'
101 bind 'set print-completions-horizontally on'
102 bind '"\C-i": self-insert'
104 # arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
105 if [[ $TERM == "xterm" ]]; then
106 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
107 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
109 bind '"\eOc": shell-forward-word'
110 bind '"\eOd": shell-backward-word'
112 # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
113 # gnome-terminal, overridden by bash, or disabled by the system
114 stty werase undef lnext undef stop undef start undef
121 # history number. History expansion is good.
123 # history file size limit, set to unlimited.
124 # this needs to be different from the default because
125 # default HISTFILESIZE is 500 and could clobber our history
127 # max commands 1 session can append/read from history
129 # my own history size limit based on lines
130 HISTFILELINES=1000000
132 # the time format display when doing the history command
133 # also, setting this makes the history file record time
134 # of each command as seconds from the epoch
135 HISTTIMEFORMAT="%I:%M %p %m/%d "
136 # consecutive duplicate lines don't go in history
137 HISTCONTROL=ignoredups
138 # works in addition to HISTCONTROL to do more flexible things
139 # it could also do the same things as HISTCONTROL and thus replace it,
143 export BC_LINE_LENGTH=0
146 # note, if I use a machine I don't want files readable by all users, set
147 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
158 for x in $HOME/bin/bash-programs-by-ian/repos/*/*-function; do
161 source $HOME/bin/identify-distro-functions
162 source $HOME/bin/semi-private # so I can share my bashrc
163 source $HOME/path_add-function
164 path_add --ifexists /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
165 path_add $HOME/bin/bash-programs-by-ian/utils
172 if [[ $- == *i* ]]; then
177 # remove any default aliases for these
178 alias ls > /dev/null 2>&1 && unalias ls
179 alias ll > /dev/null 2>&1 && unalias ll
180 alias grep > /dev/null 2>&1 && unalias grep
184 command mkdir -p "$@"
189 complete -A stopped -P '"%' -S '"' d
194 # note: gksudo is recommended for X apps because it does not set the
195 # home directory to the same.
197 if [[ $- == *i* ]]; then
198 # extra space at the end allows aliases to work
199 alias s='SUDOD="$PWD" sudo -i '
202 if [[ $EUID != 0 || $1 == -* ]]; then
213 if [[ $OS == Windows_NT ]]; then
214 alias ffs='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
216 alias j='command cygpath'
217 alias t='command cygstart'
218 alias cygstart='echo be quick, use the alias "t" instead :\)'
219 alias cygpath='echo be quick, use the alias "j" instead :\)'
227 #####################
229 #####################
231 # netselect-apt finds a fast mirror.
232 # but we need to replace the mirrors ourselves,
233 # because it doesn't do that. best it can do is
234 # output a basic sources file
235 # here we get the server it found, get the main server we use
236 # then substitute all instances of one for the other in the sources file
237 # and backup original to /etc/apt/sources.list-original.
239 debian_pick_mirror () {
240 local x=$(mktemp -d)/f # safe way to get file name without creating one
241 sudo netselect-apt -o "$x" || return 1
242 x=$(_debian_pick_mirror_grep stable "$x")
243 sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
244 sudo sed -i "s/$(_debian_pick_mirror_grep wheezy)/$x/" /etc/apt/sources.list
248 _debian_pick_mirror_grep () {
249 local x="$(grep -oP "^deb [^ ]+ $1 " ${2-/etc/apt/sources.list})"
252 # replace / with \/ so we can use it with sed
266 if type ack-grep >/dev/null 2>&1; then
272 grep -i --binary-files=without-match --color=auto "$@"
276 grep -ri --binary-files=without-match --color=auto "$@"
280 local startdir="$PWD"
284 tar cz bin/bash-programs-by-ian bin/semi-private .profile | ssh $x tar xz
287 cp ~/path_add-function ~/.bashrc ~/.bash_profile ~/.profile ~/.bash_profile ~/.inputrc .
289 tar cz path_add-function .bashrc | ssh $x tar xz
295 k() { grep -P "$@" ${HISTFILE:-~/.bash_history} | tail -n 40; }
297 calc() { echo "scale=3; $*" | bc -l; }
300 # makes it so chown -R symlink affects the symlink and its target.
302 if [[ $1 == -R ]]; then
304 command chown -h "$@"
306 command chown -RH "$@"
316 if [[ $# == 0 ]]; then
317 cp /a/bin/data/COPYING .
319 cp /a/bin/data/COPYING "$@"
325 diff --strip-trailing-cr -w "$@" # diff content
330 date "+%A, %B %d, %r" "$@"
337 envload() { # load environment from a previous: export > file
338 local file=${1:-$HOME/.${USER}_env}
339 eval "$(export | sed 's/^declare -x/export -n/')"
340 while IFS= read -r line; do
341 # declare -x makes variables local to a function
342 eval ${line/#declare -x/export}
348 # havn't tested these:
349 #file cut copy and paste, like the text buffers :)
350 _fbufferinit() { # internal use by
351 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
352 rm -rf "$my_f_tempdir"/*
356 cp "$@" "$my_f_tempdir"/
360 mv "$@" "$my_f_tempdir"/
362 fpst() { # file paste
363 [[ $2 ]] && { echo too many arguments; return 1; }
365 cp "$my_f_tempdir"/* "$target"
369 # find array. make an array of file names found by find into $x
370 # argument: find arguments
371 # return: find results in an array $x
373 while read -rd ''; do
375 done < <(find "$@" -print0);
379 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
380 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
382 gitroot || return 1 # function to set gitroot
383 builtin cd "$gitroot"
384 git symbolic-ref HEAD refs/heads/$1
390 firefox -P default "$@" >/dev/null 2>&1
394 firefox -P alt "$@" >/dev/null 2>&1
401 # horizontal row. used to break up output
402 hr() { printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $(seq $COLUMNS); }
408 # modified from ~/local/bin/git-completion.bash
409 # other completion commands are mostly taken from bash_completion package
410 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
411 || complete -o default -o nospace -F _git i
420 find . -iname '*'"$*"'*'
427 if [[ $PWD == /[iap] ]]; then
428 command ls -A --color=auto -I lost+found "$@"
430 command ls -A --color=auto "$@"
435 lld() { ll -d "$@"; }
438 low() { # make filenames all lowercase
441 y=$(tr "[A-Z]" "[a-z]" <<<"$x")
442 [[ $y != $x ]] && mv "$x" "$y"
447 lower() { # make first letter of filenames lowercase.
450 if [[ ${x::1} == [A-Z] ]]; then
451 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
452 safe_rename "$x" "$y"
458 if [[ $# != 2 ]]; then
459 echo safe_rename error: $# args, need 2 >2
461 elif [[ $1 != $2 ]]; then
463 echo Cannot rename "$1" to "$2" as it already exists.
474 safe_rename "$x" "$y"
482 # aliases would be much more compact, but they can't be used as ssh commands
483 # also, to be used in a script, you need -i which prints annoying
484 # warnings. instead, use -l in a script to source this file
485 if type -p yum > /dev/null; then
487 if [[ $EUID == 0 ]]; then
494 if [[ $EUID == 0 ]]; then
497 sudo yum -y install "$@"
501 if [[ $EUID == 0 ]]; then
509 if [[ $EUID == 0 ]]; then
516 if [[ $EUID == 0 ]]; then
517 aptitude -y install "$@"
519 sudo aptitude -y install "$@"
523 # scratch a very annoying itch.
524 # package description width as wide as the screen, and package name field small
525 # aptitude manual can't figure out how wide emacs terminal is,
526 # of course it doesn't consult the $COLUMNS variable...
527 # and in a normal terminal, it makes the package name field ridiculously big
528 # also, remove that useless dash before the description
529 if [[ $EUID == 0 ]]; then
530 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
532 sudo aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
538 # test existence / exists
542 [[ -e "$x" || -L "$x" ]] || ret=1
548 # fix root file ownership for FILE argument.
549 # check if parent or grandparent is not root and if the dir of FILE is also
550 # owned by that user, and change ownership to that user
553 if [[ $EUID == 0 ]]; then
554 te "$1" || touch "$1"
555 if [[ $(stat -c "%u" "$1") == 0 ]] ; then
556 argdir=$(getdir "$1")
557 if [[ $(stat -c "%u" "$argdir") != 0 ]] ; then
558 if ! chown "--reference=$argdir" "$1"; then
559 echo failed to fix bad ownership file permissons
567 pfind() { #find *$1* in $PATH
568 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
570 IFS=: pathArray=($PATH); unset IFS
571 find "${pathArray[@]}" -iname "*$1*"
575 pwd() { # do pwd + some other info.
576 echo "$(ll -d "$PWD") $USER@$HOSTNAME $(date +%r)"
580 pwgen() { # generate a random password, with digits & punctuation and without
582 head -c 200 /dev/urandom | tr -cd '[:graph:]' | head -c "$arg"
584 head -c 200 /dev/urandom | tr -cd '[:alnum:]' | head -c "$arg"
588 q() { # start / launch a program in the backround and redir output to null
595 exit "$@" 2>/dev/null
598 # trash-restore lists everything that has been trashed at or below CWD
599 # This picks out files just in CWD, not subdirectories,
600 # which also match grep $1, usually use $1 for a time string
601 # which you get from running restore-trash once first
605 # last condition is to not ask again for ones we skipped
606 while name="$( echo | restore-trash | gr "$PWD/[^/]\+$" | gr "$1" )" \
607 && [[ $name ]] && (( $(wc -l <<<"$name") >= nth )); do
608 name="$(echo "$name" | head -n $nth | tail -n 1 )"
609 read -p "$name [Y/n] " ask
610 if [[ ! $ask || $ask == [Yy] ]]; then
611 x=$( echo "$name" | gr -o "^\s*[0-9]*" )
612 echo $x | restore-trash > /dev/null
613 elif [[ $ask == [Nn] ]]; then
621 # rsync, root is required to keep permissions right.
622 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
623 # --no-times --delete
624 # basically, make an exact copy, use checksums instead of file times to be more accurate
625 rl() { rsync -ahvic --delete "$@"; }
626 # don't delete files on the target end which do not exist on the original end:
627 rld() { rsync -ahvic "$@"; }
628 complete -F _rsync -o nospace rld rlt fl
629 # rl without preserving modification time. for some reason I had this as default before.
630 # perhaps that reason will come up again and I will document it.
631 rlt() { rsync -ahvic --delete --no-t "$@"; }
635 # use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
640 complete -F _root_command s sb
642 # use -ll, less secure but faster.
647 # sudo redo. be aware, this command may not work right on strange distros or earlier software
649 if [[ $# == 0 ]]; then
650 sudo -E bash -c -l "$(history -p '!!')"
652 echo this command redos last history item. no argument is accepted
658 # log with script. timing is $1.t and script is $1.s
659 # -l to save to ~/typescripts/
660 # -t to add a timestamp to the filenames
662 local logdir do_stamp arg_base
663 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
666 while getopts "lt" option
669 l ) arg_base=$logdir ;;
673 shift $(($OPTIND - 1))
675 [[ -e $logdir ]] || mkdir -p $logdir
676 $do_stamp && arg_base+=$(date +%F.%T%z)
677 script -t $arg_base.s 2> $arg_base.t
679 splay() { # script replay
680 #logRoot="$HOME/typescripts/"
681 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
682 scriptreplay "$1.t" "$1.s"
689 (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
693 ts() { # start editing a new file
694 [[ $# != 1 ]] && echo "I need a filename." && return 1
696 if [[ $- != *i* ]]; then
699 if [[ $1 == *.c ]]; then
700 e '#include <stdio.h>' >"$1"
701 e '#include <stdlib.h>' >>"$1"
702 e 'int main(int argc, char * argv[]) {' >>"$1"
703 e ' printf( "hello world\n");' >>"$1"
704 e ' return 0;' >>"$1"
706 e "${1%.c}: $1" > Makefile
707 e " g++ -ggdb -std=gnu99 -o ${1%.c} $<" >> Makefile
708 e "#!/bin/bash" >run.sh
709 e "./${1%.c}" >>run.sh
711 elif [[ $1 == *.java ]]; then
712 e "public class ${1%.*} {" >"$1"
713 e ' public static void main(String[] args) {' >>"$1"
714 e ' System.out.println("Hello, world!");' >>"$1"
719 echo "#!/bin/bash" > "$1"
722 [[ $quiet ]] || g "$1"
726 tx() { # toggle set -x, and the prompt so it doesn't spam
727 if [[ $- == *x* ]]; then
729 PROMPT_COMMAND=prompt_command
740 if [[ $OS == Windows_NT ]]; then
751 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
756 if type gvfs-open &> /dev/null ; then
761 # another alternative is run-mailcap
769 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
773 hl() { # history limit. Write extra history to archive file.
774 local max_lines linecount tempfile prune_lines
775 local harchive="${HISTFILE}_archive"
776 for x in "$HISTFILE" "$harchive"; do
777 [[ -e $x ]] || { touch "$x" && echo "notice from hl(): creating $x"; }
778 if [[ ! $x || ! -e $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
779 echo "error in hl: history file \$x:$x no good"
783 history -a # save history
784 max_lines=$HISTFILELINES
785 [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
786 linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
787 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
788 if (($linecount > $max_lines)); then
789 prune_lines=$(($linecount - $max_lines))
790 head -n $prune_lines "$HISTFILE" >> "$harchive" \
791 && sed -ie "1,${prune_lines}d" $HISTFILE
795 if [[ $- == *i* ]]; then
796 # commands to run when bash exits normally
801 # temporary variables to test colorization
802 # some copied from gentoo /etc/bash/bashrc,
804 # dircolors --print-database uses its own built-in database
805 # instead of using /etc/DIR_COLORS. Try to use the external file
806 # first to take advantage of user additions.
807 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
809 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
810 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
811 [[ -z ${match_lhs} ]] \
812 && type -P dircolors >/dev/null \
813 && match_lhs=$(dircolors --print-database)
814 # test if our $TERM is in the TERM values in dircolor
815 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
818 if ${use_color} && [[ $- == *i* ]]; then
820 if [[ $XTERM_VERSION == Cygwin* ]]; then
824 underl) echo -n $'\E[4m' ;;
825 bold) echo -n $'\E[1m' ;;
826 red) echo -n $'\E[31m' ;;
827 green) echo -n $'\E[32m' ;;
828 blue) echo -n $'\E[34m' ;;
829 cyan) echo -n $'\E[36m' ;;
830 yellow) echo -n $'\E[33m' ;;
831 purple) echo -n $'\E[35m' ;;
832 nocolor) echo -n $'\E(B\E[m' ;;
841 underl) echo -n $(tput smul) ;;
842 bold) echo -n $(tput bold) ;;
843 red) echo -n $(tput setaf 1) ;;
844 green) echo -n $(tput setaf 2) ;;
845 blue) echo -n $(tput setaf 4) ;;
846 cyan) echo -n $(tput setaf 6) ;;
847 yellow) echo -n $(tput setaf 3) ;;
848 purple) echo -n $(tput setaf 5) ;;
849 nocolor) echo -n $(tput sgr0) ;; # no font attributes
859 # Try to keep environment pollution down, EPA loves us.
860 unset safe_term match_lhs use_color
872 if [[ $- == *i* ]]; then
873 # git branch/status prompt function
874 if [[ $OS != Windows_NT ]]; then
875 GIT_PS1_SHOWDIRTYSTATE=true
877 # arch source location
878 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
879 # fedora/debian source
880 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
882 # in case we didn't source git-prompt.sh
883 if ! declare -f __git_ps1 > /dev/null; then
889 # this needs to come before next ps1 stuff
890 # this stuff needs bash 4, feb 2009,
891 # old enough to no longer condition on $BASH_VERSION anymore
895 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
897 bind -m vi-command B:shell-backward-word
898 bind -m vi-command W:shell-forward-word
901 if [[ $SSH_CLIENT ]]; then
906 local return=$? # this MUST COME FIRST
908 local ps_char ps_color
910 history -a # save history
911 history -n # read any new history
912 if [[ ! $DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
913 # from the screen man page
914 if [[ $TERM == screen* ]]; then
915 local title_escape="\033]..2;"
917 local title_escape="\033]0;"
919 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
923 0) ps_color="$(get_term_color blue)"
926 1) ps_color="$(get_term_color green)"
927 ps_char="$return \\$"
929 *) ps_color="$(get_term_color yellow)"
930 ps_char="$return \\$"
933 if [[ ! -O . ]]; then # not owner
934 if [[ -w . ]]; then # writable
935 ps_color="$(get_term_color bold red)"
937 ps_color="$(get_term_color bold green)"
940 PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
942 PROMPT_COMMAND=prompt_command
949 ###########################################
950 # stuff that makes sense to be at the end #
951 ###########################################
952 if [[ "$SUDOD" ]]; then
954 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
963 # if someone exported $SOE, catch errors