3 ### Bash session things
5 ## Only do this if we haven't done it before. (Note that this guard isn't
6 ## exported, so subshells will need to make their own arrangements.)
7 if [ -z "$__mdw_bashrc" ]; then
10 ## If we've not run the main profile yet, we should do that first. It sets
11 ## up things we rely on. Also, if there's a system script, we should run
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
17 [ -r /etc/bash_completion ] && . /etc/bash_completion
18 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
20 ## Set the temporary directory again. (If we've switched users, we'll want a
21 ## different temporary directory.)
22 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
24 ###--------------------------------------------------------------------------
27 ## Only bother if the shell is interactive.
30 ## Fancy highlighting in some terminals.
31 marker=${STY+'\[
\ek
\e\\\]'}
33 linux*|screen*|xterm*|vt100*|eterm*)
34 case "$(tput bold)" in
35 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
36 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
38 rccolour="\[$(tput setaf 1)\]"
39 uncolour="\[$(tput op)\]"
43 bold='' unbold='' nl='' rccolour='' uncolour='';;
46 ## Choose the right delimiters. Highlight root prompts specially;
47 ## highlight when I'm running as some other user. Highlight when this
48 ## isn't the outermost shell on the terminal.
49 if (( EUID == 0 )); then
50 left=`echo « | iconv -f UTF-8 -t //translit`
51 right=`echo » | iconv -f UTF-8 -t //translit`
54 mdw|mwooding|nemo) u="" left="[" right="]" ;;
55 *) u="\\u@" left="{" right="}" ;;
57 if [ "$__mdw_tty" = "`tty`" ]; then
60 export __mdw_tty="`tty`"
64 ## If this session is insecure then highlight that.
65 if [ -z "$SSH_CLIENT" ] &&
66 [ "$__mdw_sechost" != "`hostname`" ]
71 ## Build the prompt string.
72 rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
73 rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
74 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$rc$marker$right$unbold"
75 PS2="$PS1 $bold>$unbold "
76 unset nl bold unbold left right sec_l sec_r marker
77 unset rccolour uncolour rc
80 ###--------------------------------------------------------------------------
81 ### Other shell tweaking.
83 ## Random shell tweaks.
92 shopt -s expand_aliases
94 if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
101 shopt -s interactive_comments
107 shopt -u shift_verbose
109 HISTCONTROL=ignorespace:erasedups
111 ## Some handy aliases.
113 alias which="command -v"
115 alias ssync="rsync -e ssh"
117 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
121 alias re="rootly $EDITOR"
122 alias pstree="pstree -hl"
123 alias cdtmp='cd ${TMPDIR-/tmp}'
124 alias pushtmp='pushd ${TMPDIR-/tmp}'
126 alias svn="svnwrap svn"
129 ###--------------------------------------------------------------------------
132 ## Arrange for `ls' output to be in colour.
133 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
134 eval `dircolors -b ~/.dircolors`
139 unalias ls 2>/dev/null || :; function ls () {
141 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
147 ## Arrange for `grep' output to be in colour.
148 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
151 declare grep=$1; shift
153 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
158 alias grep="greplike grep"
159 alias egrep="greplike egrep"
160 alias fgrep="greplike fgrep"
161 alias zgrep="greplike zgrep"
163 ## Turn off pagers inside Emacs shell buffers.
164 case "$INSIDE_EMACS" in
165 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
168 ###--------------------------------------------------------------------------
169 ### More complicated shell functions.
171 ## xt [@HOST] XTERM-ARGS
173 ## Open a terminal, maybe on a remote host.
177 local remote=${1#@} title
179 if [ $# -gt 0 ]; then
180 title="xterm [$remote] $1"
182 title="xterm [$remote]"
184 (xterm -title "$title" -e ssh $remote "$@" &)
194 ## Tweak core dumps on and off, or show the current status.
197 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
198 xoff|xn|xno) ulimit -Sc 0 ;;
202 0) echo "Core dumps disabled" ;;
203 unlimited) echo "Core dumps enabled" ;;
204 *) echo "Core dump limit is $l blocks" ;;
208 echo >&2 "usage: core [y|n]"
216 ## Set current security world to NAME. With no NAME, print the currently
219 local nfast=${NFAST_HOME-/opt/nfast}
223 echo "${NFAST_KMDATA#$nfast/kmdata-}"
228 elif [ -d "$nfast/kmdata-$1" ]; then
229 kmdata=$nfast/kmdata-$1
231 echo >&2 "world: can't find world $1"
236 0) export NFAST_KMDATA=$kmdata ;;
243 ## Fix `man' under Slowaris.
251 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
254 command man "${man[@]}"
259 ###--------------------------------------------------------------------------
262 ## path-add [VAR] DIR
264 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
265 ## it's not there already.
267 local pathvar export dir val
269 1) pathvar=PATH dir=$1 export="export PATH";;
270 2) pathvar=$1 dir=$2 export=:;;
271 *) echo >&2 "Usage: $0 [VAR] DIR";;
273 eval "val=\$$pathvar"
278 eval "$pathvar=\$val"
282 ## path-remove [VAR] DIR
284 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
285 ## error if DIR isn't in VAR.
287 local pathvar export dir val
289 1) pathvar=PATH dir=$1 export="export PATH";;
290 2) pathvar=$1 dir=$2 export=:;;
291 *) echo >&2 "Usage: $0 [VAR] DIR";;
293 eval "val=\$$pathvar"
296 :"$dir":*) val=${val#$dir:} ;;
297 *:"$dir":) val=${val%:$dir} ;;
298 *:"$dir":*) val=${val/:$dir:/:} ;;
300 eval "$pathvar=\$val"
304 ## pathhack [-f] +HACK|-HACK...
306 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
307 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
308 ## that's already on the PATH doesn't do anything unless `-f' is set, in
309 ## which case it gets moved to the beginning. With no arguments, print the
310 ## currently installed hacks.
312 if [ $# -eq 0 ]; then
317 echo ${e#$HOME/bin/hacks/}
325 while [ $# -gt 0 ]; do
344 dir=$HOME/bin/hacks/$hack
346 echo "$0: path hack $hack not found"
349 case "$arg,$force,:$PATH:" in
351 path-remove path "$dir"
354 path-remove path "$dir"
365 if [ $# -eq 0 ]; then
373 ###--------------------------------------------------------------------------
374 ### Finishing touches.
376 ## For `root' use -- some simple molly-guards.
377 if (( UID == 0 )); then
378 alias rm='rm -i' cp='cp -i' mv='mv -i'
382 ## Run any local hooks.
383 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
385 ## Close the `__mdw_bashrc' guard.
388 ###----- That's all, folks --------------------------------------------------