Bashrc Profile
~ # more .bashrc
#!/usr/bin/bash
#
# Khan .bashrc file
#
AWK=/usr/xpg4/bin/awk
CUT=/usr/bin/cut
DATE=/usr/bin/date
#echo start dot bashrc
#-----------------------
# Greeting, motd etc...
#-----------------------
# Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color
settitle ()
{
_whereme=${_whereme:="`hostname`"}
_whome=${_whome:="`id |${CUT} -d '(' -f2|${CUT} -d ')' -f1' '`"}
echo -e "\033]2;$_whome@$_whereme:$PWD\007\c"
}
telnet ()
{
if [ -n "$STY" -a -n "$1" ]; then
screen -T xterm -t "$1" //telnet "$@";
else
/usr/bin/telnet "$@";
fi
}
#ssh ()
#{
# if [ -n "$STY" -a -n "$1" ]; then
# local _HOSTNAME;
# _HOSTNAME=$( eval echo \$$# );
# /usr/bin/echo "\033]83;screen -t "$_HOSTNAME" ssh
$@\007\c";
# else
# /usr/bin/ssh "$@";
# fi
#}
#
# checking if SU to root, if so source profile to get path
#
export ID=` id | ${CUT} -f2 -d"(" | ${CUT} -f1 -d")"`
if [ ${ID} == "root" ]; then
if [ -f /root/.profile-EIS ]
then
echo aquiring root profile
. /root/.profile-EIS
fi
fi
if [ -d /usr/local/bin ]
then
PATH=/usr/local/bin:/usr/local/sbin:${PATH}:/usr/xpg4/bin
# PATH=/usr/xpg4/bin:/usr/local/bin:${PATH}
MANPATH=${MANPATH}:/usr/local/man
fi
if [ -d /home/abrook/bin ]
then
# PATH=${PATH}:/home/abrook/bin
PATH=/home/abrook/bin:${PATH}
MANPATH=${MANPATH}:/home/abrook/man
fi
if [ -d /usr/perl5/bin ]
then
PATH=${PATH}:/usr/perl5/bin
MANPATH=/usr/perl5/man:${MANPATH}
fi
if [ -d /opt/webstack/mysql/bin ]
then
PATH=${PATH}:/opt/webstack/mysql/bin
MANPATH=//opt/webstack/mysql/man:${MANPATH}
fi
if [ -f /etc/zones/index ]
then
export ZONENAME=`cat /etc/zones/index | grep -v "^#" | head -1 | awk -F\: '{print $1}'`
else
export ZONENAME="No Zones "
fi
#export PS1='\# \u@\H \w \$ '
export PS1="${BLUE}\# ${RED}\u@\h ${BLUE}\w \$ >${NC} "
#PS1="
${LOGNAME}@$(hostname):$(
# [[ "$LOGNAME" = "root" ]] && printf "${PWD/${HOME}/~}# " ||
# printf "${PWD/${HOME}/~}\$ ")'
TIME=`${DATE} +%H:%M`
if [ -f /root/server-name ]
then
servername=`cat /root/server-name`
fi
#export PS1="\# ${RED}${ZONENAME}
${BLUE}${LOGNAME}@$(hostname) $( [[ "$ID" = "root" ]] && printf " ${RED}\n\w # ${NC}\n" || printf " ${BLUE}\n\w $ ${NC}")"
export PS1="\# ${servername} ${RED}${ZONENAME}
${BLUE}${LOGNAME}@$(hostname) $( [[ "$ID" = "root" ]] && printf " ${RED}\n\w # ${NC}\n" || printf " ${BLUE}\n\
w $ ${NC}")"
source /home/abrook/bin/hcmnt
export hcmntextra='date "+%Y%m%d %R"'
export PROMPT_COMMAND='hcmnt'
PROMPT_COMMAND='hcmnt -etyl ~/.hcmnt.$(hostname).log
$LOGNAME@$HOSTNAME'#export PROMPT_COMMAND=settitle
#export PROMPT_COMMAND=settitle
#if [ $(hostname) == "abrook-desktop" ]; then
#echo "setting default proxy"
#echo "set default proxy"
#fi
#---------------
# Some settings
#---------------
ulimit -S -c 0 # Don't want any coredumps
set -o notify
set -o ignoreeof
set -o nounset
#set -o xtrace # useful for debuging
# Enable options:
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion # bash>=2.04 only
shopt -s cmdhist
shopt -s histappend histreedit histverify
shopt -s extglob # necessary for programmable completion
# Disable options:
shopt -u mailwarn
unset MAILCHECK # I don't want my shell to warn me of incoming mail
export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
export HISTIGNORE="&:bg:fg:ll:h"
export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts
# fix timouts
export TMOUT=0
export TIMEOUT=0
unset autologout
#
# Colour Codes
#PS1='[\u@TEST \w]\n \#\$ \n\
#\[\
#\e[1mBold Text\e[m\n\
#\e[4mUnderline Text\e[m\n\
#\e[5mBlink Text\e[m\n\
#\e[7mInverse Text\e[m\]\n\
#Should be normal text
#Foreground colors:
#\[\
#\e[0;30m30: Black\n\
#\e[0;31m31: Red\n\
#\e[0;32m32: Green\n\
#\e[0;33m33: Yellow\Orange\n\
#\e[0;34m34: Blue\n\
#\e[0;35m35: Magenta\n\
#\e[0;36m36: Cyan\n\
#\e[0;37m37: Light Gray\Black\n\
#\e[0;39m39: Default\n\
#Bright foreground colors:
#\e[1;30m30: Dark Gray\n\
#\e[1;31m31: Red\n\
#\e[1;32m32: Green\n\
#\e[1;33m33: Yellow\n\
#\e[1;34m34: Blue\n\
#\e[1;35m35: Magenta\n\
#\e[1;36m36: Cyan\n\
#\e[1;37m37: White\n\
#\e[0;39m39: Default\n\
#\e[m\]Background colors:
#\[\e[1;37m\e[40m40: Black\e[0;49m\n\
#\e[41m41: Red\e[0;49m\n\
#\e[42m42: Green\e[0;49m\n\
#\e[43m43: Yellow\Orange\e[0;49m\n\
#\e[44m44: Blue\e[0;49m\n\
#\e[45m45: Magenta\e[0;49m\n\
#\e[46m46: Cyan\e[0;49m\n\
#\e[47m47: Light Gray\Black\e[0;49m\n\
#\e[49m49: Default\e[m\]\n'
# Looks best on a black background.....
#if [ -z ${DISPLAY:=""|} ]; then
#echo -e "${BLUE}This is BASH ${RED}${BASH_VERSION%.*}${BLUE} - DISPLAY on ${RED}$DISPLAY${NC}\n"
#else
#echo -e "${BLUE}This is BASH ${RED}${BASH_VERSION%.*}${BLUE} ${NC}\n"
#fi
#date
#if [ -x /usr/games/fortune ]; then
# /usr/games/fortune -s # makes our day a bit more fun.... :-)
#fi
#function _exit() # function to run upon exit of shell
#{
# echo -e "${RED}Leaving Bash shell ${NC}"
#}
#trap _exit EXIT
#---------------
# Shell Prompt
#---------------
# --> Replace instances of \W with \w in prompt functions below
#+ --> to get display of full path name.
#===============================================================
#
# ALIASES AND FUNCTIONS
#
# Arguably, some functions defined here are quite big
# (ie 'lowercase') but my workstation has 512Meg of RAM, so .....
# If you want to make this file smaller, these functions can
# be converted into scripts.
#
# Many functions were taken (almost) straight from the bash-2.04
# examples.
#
#===============================================================
#-------------------
# Personnal Aliases
#-------------------
# -> Prevents accidentally clobbering files.
#alias rm='rm -i'
alias cp='cp -i'
alias sd='sudo /usr/bin/bash'
alias mv='mv -i'
# turns off noclober for all files
#set +o noclobber
alias mkdir='mkdir -p'
alias j='jobs -l'
alias which='type -all'
alias ..='cd ..'
alias path='echo -e ${PATH//:/\\n}'
alias print='/usr/bin/lp -o nobanner -d $LPDEST' # Assumes LPDEST is defined
alias pjet='enscript -h -G -fCourier9 -d $LPDEST' # Pretty-print using enscript
alias du='du -kh'
alias df='df -kh'
# The 'ls' family (this assumes you use the GNU ls)
alias la='ls -Al' # show hidden files
#alias ls='ls -hF --color' # add colors for filetype recognition
alias lx='ls -lXB' # sort by extension
alias lk='ls -lSr' # sort by size
alias lc='ls -lcr' # sort by change time
#alias lu='ls -lur' # sort by access time
alias lr='ls -lR' # recursive ls
alias lt='ls -ltr' # sort by date
alias lm='ls -al |more' # pipe through 'more'
alias tree='tree -Csu' # nice alternative to 'ls'
# tailoring 'less'
alias more='less'
export PAGER=less
export LESSCHARSET='latin1'
export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-' # Use this if lesspipe.sh exists
export LESS='-i -n -w -z-4 -g -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'
# spelling typos - highly personnal :-)
alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'
alias h='history | tail -20'
#
# setup profile aliases
#
for i in `/usr/bin/profiles -l | grep -v ":$" | grep -v '*' | sed 's/^[ \t] *//' | grep "^/.*" | ${CUT} -f1 -d " "`
do
alias `basename $i`="pfexec `basename $i`"
done
alias sur='su primaryadmin'
#
#
#
#alias tapewwn="luxadm -e dump_map `luxadm -e port | grep -v NOT | grep 60000 | ${AWK} '{print $1}'` | grep Tape | ${AWK} '{print $4}'"
#alias tapewwn="luxadm -e port | grep -v NOT | grep 60000 | ${AWK} '{print $1}'"
#-----------------------------------
# File & strings related functions:
#-----------------------------------
# Find a file with a pattern in name:
function ff() { find . -type f -iname '*'$*'*' -ls ; }
# Find a file with pattern $1 in name and Exe/usr/bin/cute $2 on it:
function fe() { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; }
# find pattern in a set of filesand highlight them:
function fstr()
{
OPTIND=1
local case=""
local usage="fstr: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
while getopts :it opt
do
case "$opt" in
i) case="-i " ;;
*) echo "$usage"; return;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" -lt 1 ]; then
echo "$usage"
return;
fi
local SMSO=$(tput smso)
local RMSO=$(tput rmso)
find . -type f -name "${2:-*}" -print0 | xargs -0 grep -sn ${case} "$1" 2>&- | \
sed "s/$1/${SMSO}\0${RMSO}/gI" | more
}
function cuttail() # /usr/bin/cut last n lines in file, 10 by default
{
nlines=${2:-10}
sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1
}
function lowercase() # move filenames to lowercase
{
for file ; do
filename=${file##*/}
case "$filename" in
*/*) dirname==${file%/*} ;;
*) dirname=.;;
esac
nf=$(echo $filename | tr A-Z a-z)
newname="${dirname}/${nf}"
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
echo "lowercase: $file --> $newname"
else
echo "lowercase: $file not changed."
fi
done
}
function file-swap() # swap 2 filenames around
{
local TMPFILE=tmp.$$
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
}
#-----------------------------------
# Process/system related functions:
#-----------------------------------
#function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
#function pp() { my_ps f | ${AWK} '!/${AWK}/ && $0~var' var=${1:-".*"} ; }
# This function is roughly the same as 'killall' on linux
# but has no equivalent (that I know of) on Solaris
function killps() # kill by process name
{
local pid pname sig="-TERM" # default signal
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: killps [-SIGNAL] pattern"
return;
fi
if [ $# = 2 ]; then sig=$1 ; fi
for pid in $(my_ps| ${AWK} '!/${AWK}/ && $0~pat { print $1 }' pat=${!#} ) ; do
pname=$(my_ps | ${AWK} '$1~var { print $5 }' var=$pid )
if ask "Kill process $pid <$pname> with signal $sig?"
then kill $sig $pid
fi
done
}
function my_ip() # get IP adresses
{
MY_IP=$(/sbin/ifconfig -a | ${AWK} '/inet/ { print $2 } ' | sed -e s/addr://)
}
function ii() # get current host related info
{
echo -e "\nYou are logged on ${RED}$_HOSTNAME"
echo -e "\nAdditionnal information:$NC " ; uname -a
echo -e "\n${RED}Users logged on:$NC " ; w -h
echo -e "\n${RED}Current date :$NC " ; ${DATE}
echo -e "\n${RED}Machine stats :$NC " ; uptime
my_ip 2>&- ;
echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
echo
}
# Misc utilities:
function repeat() # repeat n times command
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do # --> C-like syntax
eval "$@";
done
}
function ask()
{
echo -n "$@" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
function change()
{
STAMP=`${DATE} +bak.%m-%d-%H-%M`
cp $1 $1.${STAMP}
vi $1
}
#=========================================================================
#
# PROGRAMMABLE COMPLETION - ONLY SINCE BASH-2.04
# Most are taken from the bash 2.05 documentation and from Ian McDonalds
# 'Bash completion' package (
http://www.caliban.org/bash/index.shtml#completion)
# You will in fact need bash-2.05a for some features
#
#=========================================================================
if [ "${BASH_VERSION%.*}" \< "2.05" ]; then
echo "You will need to upgrade to version 2.05 for programmable completion"
return
fi
shopt -s extglob # necessary
set +o nounset # otherwise some completions will fail
complete -A hostname rsh rcp telnet rlogin r ftp ping disk
complete -A export printenv
complete -A variable export local readonly unset
complete -A enabled builtin
complete -A alias alias unalias
complete -A function function
complete -A user su mail finger
complete -A helptopic help # currently same as builtins
complete -A shopt shopt
complete -A stopped -P '%' bg
complete -A job -P '%' fg jobs disown
complete -A directory mkdir rmdir
complete -A directory -o default cd
# Compression
complete -f -o default -X '*.+(zip|ZIP)' zip
complete -f -o default -X '!*.+(zip|ZIP)' unzip
complete -f -o default -X '*.+(z|Z)' compress
complete -f -o default -X '!*.+(z|Z)' uncompress
complete -f -o default -X '*.+(gz|GZ)' gzip
complete -f -o default -X '!*.+(gz|GZ)' gunzip
complete -f -o default -X '*.+(bz2|BZ2)' bzip2
complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2
# Postscript,pdf,dvi.....
complete -f -o default -X '!*.ps' gs ghostview ps2pdf ps2ascii
complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.pdf' acroread pdf2ps
complete -f -o default -X '!*.+(pdf|ps)' gv
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps
# Multimedia
complete -f -o default -X '!*.+(jp*g|gif|xpm|png|bmp)' xv gimp
complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321
complete -f -o default -X '!*.+(ogg|OGG)' ogg123
complete -f -o default -X '!*.pl' perl perl5
# This is a 'universal' completion function - it works when commands have
# a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
_get_longopts ()
{
$1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \
grep ^"$2" |sort -u ;
}
_longopts_func ()
{
case "${2:-*}" in
-*) ;;
*) return ;;
esac
case "$1" in
\~*) eval cmd="$1" ;;
*) cmd="$1" ;;
esac
COMPREPLY=( $(_get_longopts ${1} ${2} ) )
}
complete -o default -F _longopts_func configure bash
complete -o default -F _longopts_func wget id info a2ps ls recode
_make_targets ()
{
local mdef makef gcmd cur prev i
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
# if prev argument is -f, return possible filename completions.
# we could be a little smarter here and return matches against
# `makefile Makefile *.mk', whatever exists
case "$prev" in
-*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;;
esac
# if we want an option, return the possible posix options
case "$cur" in
-) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
esac
# make reads `makefile' before `Makefile'
if [ -f makefile ]; then
mdef=makefile
elif [ -f Makefile ]; then
mdef=Makefile
else
mdef=*.mk # local convention
fi
# before we scan for targets, see if a makefile name was specified
# with -f
for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
if [[ ${COMP_WORDS[i]} == -*f ]]; then
eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion
break
fi
done
[ -z "$makef" ] && makef=$mdef
# if we have a partial word to complete, restrict completions to
# matches of that word
if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
# if we don't want to use *.mk, we can take out the cat and use
# test -f $makef and input redirection
COMPREPLY=( $(cat $makef 2>/dev/null | ${AWK} 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
}
complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
# cvs(1) completion
_cvs ()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then
COMPREPLY=( $( compgen -W 'add admin checkout commit diff \
export history import log rdiff release remove rtag status \
tag update' $cur ))
else
COMPREPLY=( $( compgen -f $cur ))
fi
return 0
}
complete -F _cvs cvs
_killall ()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# get a list of processes (the first sed evaluation
# takes care of swapped out processes, the second
# takes care of getting the basename of the process)
COMPREPLY=( $( /usr/bin/ps -u $USER -o comm | \
sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
${AWK} '{if ($0 ~ /^'$cur'/) print $0}' ))
return 0
}
complete -F _killall killall killps
# A meta-command completion function for commands like sudo(8), which need to
# first complete on a command, then complete according to that command's own
# completion definition - currently not quite foolproof (e.g. mount and umount
# don't work properly), but still quite useful - By Ian McDonald, modified by me.
_my_command()
{
local cur func cline cspec
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [ $COMP_CWORD = 1 ]; then
COMPREPLY=( $( compgen -c $cur ) )
elif complete -p ${COMP_WORDS[1]} &>/dev/null; then
cspec=$( complete -p ${COMP_WORDS[1]} )
if [ "${cspec%%-F *}" != "${cspec}" ]; then
# complete -F
#
# COMP_CWORD and COMP_WORDS() are not read-only,
# so we can set them before handing off to regular
# completion routine
# set current token number to 1 less than now
COMP_CWORD=$(( $COMP_CWORD - 1 ))
# get function name
func=${cspec#*-F }
func=${func%% *}
# get current command line minus initial command
cline="${COMP_LINE#$1 }"
# split current command line tokens into array
COMP_WORDS=( $cline )
$func $cline
elif [ "${cspec#*-[abcdefgjkvu]}" != "" ]; then
# complete -[abcdefgjkvu]
#func=$( echo $cspec | sed -e 's/^.*\(-[abcdefgjkvu]\).*$/\1/' )
func=$( echo $cspec | sed -e 's/^complete//' -e 's/[^ ]*$//' )
COMPREPLY=( $( eval compgen $func $cur ) )
elif [ "${cspec#*-A}" != "$cspec" ]; then
# complete -A
func=${cspec#*-A }
func=${func%% *}
COMPREPLY=( $( compgen -A $func $cur ) )
fi
else
COMPREPLY=( $( compgen -f $cur ) )
fi
}
complete -o default -F _my_command nohup exec eval trace truss strace sotruss gdb
complete -o default -F _my_command command type which man nice
# Given a PATH like string and a separator, remove duplicate entries
removeDups() # string sep
{
if [ "${osname}" = "windows" ] ; then
printf "%s\n" "$1" | \
sed -e
's@\\@/@g' | \
${AWK} -F"$2" \
'{ \
a[toupper($1)]; \
printf "%s",$1; \
for(i=2;i<=NF;i++){ \
if(!(toupper($i) in a)){ \
a[toupper($i)]; \
printf "%s%s",FS,$i; \
} \
}; \
printf "\n"; \
}'
else
printf "%s\n" "$1" | \
${AWK} -F"$2" \
'{ \
a[$1]; \
printf "%s",$1; \
for(i=2;i<=NF;i++){ \
if(!($i in a)){ \
a[$i]; \
printf "%s%s",FS,$i; \
} \
}; \
printf "\n"; \
}'
fi
}
# Need particular AWK
sep=":"
# Get new path setting
newpath=`removeDups "${PATH}" "${sep}"`
newmanpath=`removeDups "${MANPATH}" "${sep}"`
# Redefine your PATH setting
if [ "${PATH}" != "${newpath}" ] ; then
# echo "# Resetting PATH to remove duplicates"
PATH_ORIGINAL="${PATH}"
MANPATH_ORIGINAL="${MANPATH}"
export PATH_ORIGINAL
export MANPATH_ORIGINAL
PATH="${newpath}"
MANPATH="${newmanpath}"
export MANPATH
export PATH
fi
#echo end dot bashrc