User:Pwagstro/.bashrc

From WPLUG
< User:Pwagstro
Revision as of 22:56, 12 December 2008 by Pwagstro (talk | contribs) (bashtastic!)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


My .bash_profile

This is my primary file. It uses some host detection to figure out what files it should load for the colors of the prompt and then sets up most of my environment variables. It's starting to show it's age from when I switched to bash back in 1997. Ooohhh, compiler optimizations for 32 bit sparc platforms! There are some nifty features in this that others may like. First, it's got a wicked cool prompt, well, I thought so back in 1998 when I made it. Second, I can modify and add directories to the permanent path from the command line by using the addpath command.

#-----------------------------------------------------------------------------
# /home/patrick/.bash_profile
#-----------------------------------------------------------------------------
# Patrick Wagstrom's .bash_profile file
#-----------------------------------------------------------------------------
# copyright © 1997-2008 Patrick Wagstrom
# patrick@wagstrom.net
#----------------------------------------------------------------------------- 

##############################################################################
# let everything know that we've loaded the bash_profile, this also helps to
# only load the file once
#
if [ $BASHPROFILE_LOADED ]; then
	echo "~/.bash_profile already loaded.  To reload use bashreload"
	return;
fi
BASHPROFILE_LOADED=1

##############################################################################
# set some initial variables about the operating system
#
OS=`uname`
ARCH=`uname -m`
# CPU=`arch -s`

##############################################################################
# first get the hostname
#
case "$OS" in
	OpenBSD)
		HOST=`hostname -s`
		;;
	Linux)
		HOST=`uname -n`
		;;
	SunOS)
		HOST=`hostname`
		;;
	*)
		HOST=`echo $HOSTNAME | sed "s/\([a-z]*\)\..*/\1/"`
		;;
esac

##############################################################################
# set up the configuration directory
#
if [ -d $HOME/configs ]; then 
	CONFDIR="$HOME/configs/bash"
else
	CONFDIR="$HOME/Documents/personal/configs/bash"
fi

HOSTCONF="$CONFDIR/$HOST" 

##############################################################################
# load the .bashrc file
#
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

##############################################################################
# set up the colors for each terminal
#
ESC="\033["
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
BROWN="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
MAGENTA="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
WHITE="\[\033[0;37m\]"
LIGHTBLACK="\[\033[1;30m\]"
LIGHTRED="\[\033[1;31m\]"
LIGHTGREEN="\[\033[1;32m\]"
YELLOW="\[\033[1;33m\]"
LIGHTBLUE="\[\033[1;34m\]"
LIGHTMAGENTA="\[\033[1;35m\]"
LIGHTCYAN="\[\033[1;36m\]"
LIGHTWHITE="\[\033[1;37m\]" 

case "$HOST" in
	dreams)
		COLOR1=$LIGHTGREEN
		COLOR2=$LIGHTBLUE
		;;
	visions)
		COLOR1=$LIGHTRED
		COLOR2=$YELLOW
		;;
	apollo)
		COLOR1=$LIGHTBLUE
		COLOR2=$YELLOW
		;;
	host106)
		COLOR1=$LIGHTCYAN
		COLOR2=$WHITE
		;;
	scissors)
		COLOR1=$BROWN
		COLOR2=$CYAN
		;;
	aspirations)
		COLOR1=$BLUE
		COLOR2=$WHITE
		;;
	*)
		COLOR1=$LIGHTBLUE
		COLOR2=$RED
esac

if [ -f $HOSTCONF/promptcolors ]; then
	. $HOSTCONF/promptcolors
fi

REGEXDIR=`perl -e '$a = $ENV{"HOME"}; $a =~ s/\//\\\\\//g; print $a'`
INTERACTIVE=`echo $- | grep i`
if [ $INTERACTIVE ]; then
    export INTERACTIVE="TRUE"
fi

bashreload ()
{
	unset BASHPROFILE_LOADED
	. ~/.bash_profile
}

dpms_set ()
{
	xset +dpms
	xset dpms 600 1200 1800
}

# always append history
shopt -s histappend
prompt ()
{
    # save the history
    history -a
    PLEFT=`echo $PWD | sed "s/$REGEXDIR\//\~\//"`
    if [ $TERM != 'dumb' ]; then
        # export PS1="$PL\n$COLOR1[$COLOR2\u$COLOR1@$COLOR2\h$COLOR1]$WHITE "
        export PS1="$COLOR1[$COLOR2\w$COLOR1]\n$COLOR1[$COLOR2\u$COLOR1@$COLOR2\h$COLOR1]$WHITE "
        if [ $TERM == 'xterm' ] || [ $TERM == 'dtterm' ]; then
            # set the information in the window
            echo -ne '\033]0;'$USER'@'$HOST':'$PLEFT' \007'
        fi
    else
        # export PS1="[$PLEFT]\n[\u@\h] "
        export PS1="\s-\v\$"
        export PS2=">"
    fi
	unset PL
	unset PLEFT
}

promptRV ()
{
    # save the history
    history -a
    PLEFT=`echo $PWD | sed "s/$REGEXDIR\//\~\//"`
    if [ $TERM != 'dumb' ]; then
        # export PS1="$PL\n$COLOR1[$COLOR2\u$COLOR1@$COLOR2\h$COLOR1]$WHITE "
        export PS1="$COLOR1[$COLOR2\w$COLOR1]\n$COLOR1[$COLOR2\u$COLOR1@$COLOR2\h$COLOR1]$BLACK "
        if [ $TERM == 'xterm' ] || [ $TERM == 'dtterm' ]; then
            # set the information in the window
            echo -ne '\033]0;'$USER'@'$HOST':'$PLEFT' \007'
        fi
    else
        export PS1="\s-\v\$"
        export PS2=">"
    fi
	unset PL
	unset PLEFT
}


add_colons ()
{
	/bin/cat $1 | grep -v "^#" | /usr/bin/tr "\012" ":"
}

pathadd ()
{
	for p in $*
	do
		echo $p >> ~/.bash_path
	done
	export PATH=`add_colons ~/.bash_path`
}

nkill ()
{
	ps aux | grep $1 | tr -s ' ' | cut -d ' ' -f 2 | xargs -iX kill -9 X
}

rv ()
{
	PROMPT_COMMAND=promptRV
}

load_java ()
{
	JAVA_PATH=$1
	if [ -d $JAVA_PATH ]; then
		for i in `find ${JAVA_PATH} -maxdepth 1 -type l -print`; do 
			PATH=${PATH}:${i}/bin
		done
		export PATH
		for i in $JAVA_HOME/jre/lib/*.jar; do 
			CLASSPATH=${CLASSPATH}:${i}
		done
		export CLASSPATH
	fi
	export PATH=$JAVA_HOME/bin:$PATH
} 

if [ $TERM != "dumb" ]; then
    PROMPT_COMMAND=prompt
else
    prompt
    unset $PROMPT_COMMAND
	unset PROMPT_COMMAND
	# stty -echo
	stty icanon erase ^H
fi

export BASH_ENV=$HOME/.bashrc
export USERNAME=$USER

##############################################################################
# if we have these helper files, then load them
#
if [ -f $HOSTCONF/bash_path ]; then
	export PATH=`add_colons $HOSTCONF/bash_path`
elif [ -f ~/.bash_path ]; then
	export PATH=`add_colons ~/.bash_path`
else
	export PATH="$HOME/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$HOME/usr/bin"
fi

if [ -f $HOSTCONF/bash_libs ]; then
	export LD_LIBRARY_PATH=`add_colons $HOSTCONF/bash_libs`
elif [ -f ~/.bash_libs ]; then
	export LD_LIBRARY_PATH=`add_colons ~/.bash_libs`
fi

# PostgreSQL stuff
export PGDATA=/usr/local/pgsql/data

# CVS and RSync configuration
# export CVSROOT=":ext:patrick@wagstrom.net:/home/cvs/patrick"
if ( [ -d /home/patrick/cvs ] && [ $HOST == 'dreams' ] ); then
	export CVSROOT="/home/patrick/cvs"
else
	export CVSROOT=":ext:patrick@dreams.wagstrom.net:/home/patrick/cvs"
fi

if [ -f /usr/local/bin/ssh ]; then 
	SSH='/usr/local/bin/ssh'
elif [ -f /usr/bin/ssh ]; then 
	SSH='/usr/bin/ssh'
fi
export CVS_RSH="$SSH"
export RSYNC_RSH="$SSH"
if [ -z $EDITOR ]; then
	export EDITOR="vim"
fi

# export MANPATH=$MANPATH:/usr/local/pgsql/man
export TZ="America/New_York"

##############################################################################
# keychain - ssh key manager
#
KEYCHAIN=$(which keychain 2> /dev/null | grep -v "Command not found")
if ( [ $TERM == 'xterm' ] || [ $TERM == 'linux' ] ) && [ -n "$KEYCHAIN" ]; then
	FILES=""
	for fn in ~/.ssh/id_dsa ~/.ssh/id_rsa; do
		if [ -f $fn ]; then
			FILES="$FILES $fn"
		fi
	done
	$KEYCHAIN -q $FILES
	if [ -f ~/.keychain/${HOSTNAME}-sh ]; then
		. ~/.keychain/${HOSTNAME}-sh
	else
		. ~/.ssh-agent-${HOSTNAME}
	fi
fi

##############################################################################
# java setup
#
export JAVA_HOME=""
for x in /usr/lib/jvm/java-6-sun \
         /usr/lib/jvm/java-5-sun \
         /usr/local/java/jre1.5.0_06 \
         /usr/local/java/jdk1.6.0 \
         /usr/local/java/j2sdk1.4.2_11 \
         /usr/lib/j2se/1.4 \
         /usr/lib/j2se/1.5 \
         /usr/java/jdk1.5 \
         /usr/java/jdk1.5.0 \
         /usr/java/jdk1.4 \
         /usr/java/j2sdk1.4.2_02 \
         /usr/java/j2sdk1.4.0 \
         /usr/java/jdk1.3.1_02 \
         /usr/j2sdk1.4.1_01
do
	if [ -d $x ]; then
		export JAVA_HOME="$x"
		export JDK_HOME=$JAVA_HOME
		break
	fi
done

if [ $JAVA_HOME ]; then
	export PATH="$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin"
fi

if [ -d  /usr/local/java/JMF-2.1.1e ]; then
	export JMFHOME="/usr/local/java/JMF-2.1.1e"
	export CLASSPATH="$JMFHOME/lib/jmf.jar:.:${CLASSPATH}"
	export LD_LIBRARY_PATH=$JMFHOME/lib:${LD_LIBRARY_PATH}
fi

##############################################################################
# compiler optimizations
#
case "$ARCH" in 
	athlon)
		# export CFLAGS="-O2 -mcpu=athlon -march=athlon -funroll-loops -mmmx -m3dnow"
		export CFLAGS="-O2 -mcpu=athlon -march=athlon -funroll-loops -mmmx"
		# export RPM_OPT_FLAGS="-O2 -march=athlon -mcpu=athlon -mmmx -m3dnow"
		export RPM_OPT_FLAGS="-O2 -march=athlon -mcpu=athlon -mmmx"
		;;
	sparc)
		export CFLAGS="-02"
		;;
	sparc64)
		export CFLAGS="-mcpu=v8 -mtune=v9 -O2 -pipe"
		;;
	k6)
		export CFLAGS="-02 -march=k6 -funroll-loops -mmmx"
		;;
	*)
		export CFLAGS="-O2 -funroll-loops"
esac
export CPPFLAGS=$CFLAGS
export CXXFLAGS=$CFLAGS

##############################################################################
# irc settings
#
export IRCNAME="pridkett"
export IRCUSER="pridkett"
export IRCNICK="pridkett"
export IRCSERVER="irc.ipv6.homelien.no"

##############################################################################
# python settings
#
PYTHONPATH=""
PATHS="scarlet support cvsminer"
for x in ${PATHS}; do
	if [ -d $HOME/Documents/$x/src ]; then
		PYTHONPATH="$PYTHONPATH:$HOME/Documents/$x/src"
	elif [ -d $HOME/code/$x/src ]; then
		PYTHONPATH="$PYTHONPATH:$HOME/code/$x/src"
	fi
done
PYTHONPATH="$PYTHONPATH:$HOME/code/dynet"
PYTHONPATH=$(echo $PYTHONPATH | sed -e 's/^://')
if [ -d $HOME/src/dbfpy ]; then
	PYTHONPATH=$PYTHONPATH:$HOME/src/dbfpy
fi
export PYTHONPATH

load_java /usr/local/java
# export MONO_PATH="/usr/lib:/usr/local/lib"
export PATH=$JAVA_HOME/bin:$PATH

# colors for man pages via less
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

# -*- mode: shell-script-mode -*-

My .bashrc

This is my secondary file. Basically it only sets up a few aliases across all machines and then loads a set of host specific aliases.

#-----------------------------------------------------------------------------
# /home/patrick/.bashrc
#-----------------------------------------------------------------------------
# Patrick Wagstrom's .bashrc file
# User specific aliases and functions
#-----------------------------------------------------------------------------
# copyright © 1997-2008 Patrick Wagstrom
# patrick@wagstrom.net
#-----------------------------------------------------------------------------

where ()
{
	f=`which $1 2> /dev/null | cut -d ' ' -f 1 | grep -v "which:" | grep -v "$1:"` 
	if [ -z $f ]; then
		f='FileNotFound'
	fi
	echo $f
 	unset f
}

##############################################################################
# host specific aliases
#

OS=`uname`
case "$OS" in
	OpenBSD) 
		HOST=`hostname -s`
		;;
	Linux)
		HOST=`uname -n`
		;;
	SunOS)
		HOST=`hostname`
		;;
	*)
		HOST=`echo $HOSTNAME | sed "s/\([a-z]*\)\..*/\1/"`
		;;
esac

##############################################################################
# set up the configuration directory
#
if [ -d $HOME/configs ]; then
 	CONFDIR="$HOME/configs/bash"
else
	CONFDIR="$HOME/Documents/personal/configs/bash"
fi

HOSTCONF="$CONFDIR/$HOST"
##############################################################################
# load the .bashrc file
#
if [ -f /etc/bashrc ]; then 
	. /etc/bashrc
fi
if [ -f $HOSTCONF/bashrc ]; then
	. $HOSTCONF/bashrc
fi


##############################################################################
# universal aliases
#
if [ "x$(grep --v 2>&1 | grep GNU)" != "x" ]; then 
	if [ $TERM != "dumb" ]; then 
		alias grep='grep --color=always'
		alias egrep='egrep --color=always'
	fi
fi

alias ls="ls --color=auto"
export LESS=-r

# other helpful stuff
alias emacsnox='TERM="xterm-256color" emacs -nw'
alias ec='TERM="xterm-256color" emacsclient -t'