360 lines
9.4 KiB
Bash
Executable File
360 lines
9.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# ~/.bashrc skeleton
|
|
# ~/.bashrc runs ONLY on non-login subshells! (different from ksh)
|
|
# add lines here very carefully as this may execute when you don't i
|
|
# expect them to
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#echo "BASHRC has run"
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Source global definitions
|
|
#
|
|
if [ -f /etc/bashrc ]; then
|
|
. /etc/bashrc
|
|
else
|
|
if ! shopt -q login_shell ; then # We're not a login shell
|
|
for i in /etc/profile.d/*.sh; do
|
|
if [ -r "$i" ]; then
|
|
if [ "$PS1" ]; then
|
|
. $i
|
|
else
|
|
. $i &>/dev/null
|
|
fi
|
|
fi
|
|
done
|
|
unset i
|
|
fi
|
|
fi
|
|
|
|
umask 002
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Find out system details.
|
|
#
|
|
os=`uname -o` #GNU/Linux AIX Solaris
|
|
cpu=`uname -p` #x86_64 powerpc sparc
|
|
kernel=`uname -s` #Linux AIX SunOS
|
|
kversion=`uname -v`
|
|
krelease=`uname -r`
|
|
hostnm=`hostname`
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Bash history
|
|
#
|
|
|
|
# don't put duplicate lines or lines starting with space in the history.
|
|
# See bash(1) for more options
|
|
HISTCONTROL=ignoreboth
|
|
# append to the history file, don't overwrite it
|
|
shopt -s histappend
|
|
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
|
|
HISTSIZE=1000
|
|
HISTFILESIZE=5000
|
|
# check the window size after each command and, if necessary,
|
|
# update the values of LINES and COLUMNS.
|
|
shopt -s checkwinsize
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Terminal colors if interactive
|
|
#
|
|
# to see colors use
|
|
# for i in {0..255}; do echo $(tput setaf $i)$i; done
|
|
#
|
|
if [ -n "$PS1" ]; then
|
|
#
|
|
# If there is no terminal info in the system for current terminal but there is
|
|
# one in local terminfo then use it.
|
|
#
|
|
#tput -T $TERM longname 2>/dev/null >/dev/null
|
|
#if [ "$?" -ne "0" ] && [ -f $HOME/.terminfo/$kernel/${TERM:0:1}/$TERM ]; then
|
|
# export TERMINFO=$HOME/.terminfo/$kernel;
|
|
#fi
|
|
#
|
|
# Number of colors and standard colors.
|
|
#
|
|
c_num=$(tput colors)
|
|
#
|
|
# bold foreground
|
|
#cbf_red=$(tput bold; tput setaf 1)
|
|
#
|
|
# Load better colors for ls.
|
|
#
|
|
if [ $c_num -ge 256 ] && [ -f ~/.dir_colors ]; then
|
|
eval $(dircolors ~/.dir_colors)
|
|
fi
|
|
|
|
function term_colors(){
|
|
for i in {0..15}; do
|
|
for j in {0..15}; do
|
|
c=$((i*16+j));
|
|
printf " $(tput setab $c)%04d" $c;
|
|
done;
|
|
printf "$(tput sgr0)\n";
|
|
done
|
|
for i in {0..15}; do
|
|
for j in {0..15}; do
|
|
c=$((i*16+j));
|
|
printf " $(tput setaf $c)%04d" $c;
|
|
done;
|
|
printf "$(tput sgr0)\n";
|
|
done
|
|
}
|
|
fi
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Prompt if interactive
|
|
#
|
|
if [ -n "$PS1" ]; then
|
|
#
|
|
# Set better colors if available.
|
|
#
|
|
if [ $c_num -ge 256 ]; then
|
|
c_reset="$(tput sgr0)"
|
|
c_user="$(tput setaf 203)" #Some bright color
|
|
c_host="$(tput setaf 112)" #Green
|
|
c_cwd="$(tput setaf 99)"
|
|
c_time="$(tput setaf 33)" #Light blue
|
|
c_branch="$(tput setaf 138)"
|
|
c_error="$(tput setaf 203)" #Red
|
|
c_info="$(tput setaf 220)" #Yellow
|
|
else
|
|
c_reset="$(tput sgr0)"
|
|
c_user="$(tput setaf 1)" #
|
|
c_host="$(tput setaf 2)" #Green
|
|
c_cwd="$(tput setaf 7)" #White
|
|
c_time="$(tput setaf 6)" #Cyan
|
|
c_branch="$(tput setaf 6)" #Cyan
|
|
c_error="$(tput setaf 1)" #Red
|
|
c_info="$(tput setaf 3)" #Yellow
|
|
fi
|
|
|
|
[ -n "$(which svn)" ] && prompt_check_svn=true
|
|
[ -n "$(which git)" ] && prompt_check_git=true
|
|
|
|
export PROMPT_COMMAND="__prompt_command"
|
|
function __prompt_command()
|
|
{
|
|
#
|
|
# get exit code before any command is run.
|
|
# TODO: doesn't work on pi.
|
|
#
|
|
local exit_code="$?"
|
|
prompt_extra=""
|
|
if [ "$USER" != "vkhachatrya5" -a "$USER" != "vahagn" ]; then
|
|
PS1="\[$c_user\]\u@" # username@
|
|
else
|
|
PS1=""
|
|
fi
|
|
PS1+="\[$c_host\]\h:" # hostname
|
|
PS1+="\[$c_time\]\t" # time
|
|
PS1+=" \[$c_cwd\]\w" # working_dir
|
|
#
|
|
# git branch if exist
|
|
#
|
|
if [ -n "$prompt_check_git" ]; then
|
|
local git_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null | tr -d ' ')
|
|
if [ -n "$git_branch" ]; then
|
|
PS1+="\[$c_branch\]/$git_branch"
|
|
fi
|
|
fi
|
|
#
|
|
# svn branch if exist
|
|
#
|
|
if [ -n "$prompt_check_svn" ]; then
|
|
local svn_rev=$(svn info . 2> /dev/null | nawk -F ':' '/Revision:/ { print $2 }')
|
|
if [ -n "$svn_rev" ]; then
|
|
svn_text="@$svn_rev"
|
|
PS1+="\[$c_branch\]\$svn_text" # svn revision
|
|
fi
|
|
fi
|
|
#
|
|
# exit code if not zero
|
|
#
|
|
if [ "$exit_code" -ne "0" ]; then
|
|
PS1+=",\[$c_error\]rc=$exit_code"
|
|
fi
|
|
#
|
|
# Python venv.
|
|
#
|
|
local jobs_bg=$(jobs -r | wc -l | tr -d ' ')
|
|
if [ -n "$VIRTUAL_ENV" ]; then
|
|
# PS1+=",\[$c_info\]$(realpath --relative-to=$PWD $VIRTUAL_ENV)"
|
|
PS1+=",\[$c_info\]$(basename $VIRTUAL_ENV)"
|
|
fi
|
|
#
|
|
# Info about jobs.
|
|
#
|
|
local jobs_bg=$(jobs -r | wc -l | tr -d ' ')
|
|
if [ $jobs_bg -gt 0 ]; then
|
|
PS1+=",\[$c_info\]bg=$jobs_bg"
|
|
fi
|
|
#
|
|
# Info about jobs.
|
|
#
|
|
local jobs_stopped=$(jobs -s | wc -l | tr -d ' ')
|
|
if [ $jobs_stopped -gt 0 ]; then
|
|
PS1+=",\[$c_info\]stp=$jobs_stopped"
|
|
fi
|
|
|
|
|
|
#
|
|
# Colors should be set here and surrounded with \[\]otherwise bash fails to
|
|
# calculate prompt length. The text is set through PROMPT_COMMAND
|
|
PS1+="\[$c_reset\]\$ " # reset colors and print $
|
|
}
|
|
# Turn on checkwinsize
|
|
shopt -s checkwinsize
|
|
fi
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# User specific environment and startup programs
|
|
#
|
|
|
|
#
|
|
# Set my paths.
|
|
#
|
|
if [ -e $HOME/local ]; then
|
|
MYLOCAL=$HOME/local
|
|
elif [ $kernel == Linux ] && [ -e $HOME/local-lnx ]; then
|
|
MYLOCAL=$HOME/local-lnx
|
|
elif [ $kernel == SunOS ] && [ -e $HOME/local-sun ]; then
|
|
MYLOCAL=$HOME/local-sun
|
|
fi
|
|
if [ -e $MYLOCAL/bin ]; then
|
|
export PATH=$MYLOCAL/bin:$PATH
|
|
fi
|
|
|
|
MYSCRIPTS=$HOME/devel/scripts
|
|
if [ -e $MYSCRIPTS/bin ]; then
|
|
export PATH=$MYSCRIPTS/bin:$PATH
|
|
fi
|
|
|
|
if [ -e $HOME/.local/bin ]; then
|
|
export PATH=$HOME/.local/bin:$PATH
|
|
fi
|
|
|
|
#
|
|
# VIM
|
|
#
|
|
export EDITOR=vim
|
|
export LESS=-R
|
|
|
|
#
|
|
# enable programmable completion features (you don't need to enable
|
|
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
|
|
# sources /etc/bash.bashrc).
|
|
#
|
|
if ! shopt -oq posix; then
|
|
if [ -f /usr/share/bash-completion/bash_completion ]; then
|
|
. /usr/share/bash-completion/bash_completion
|
|
elif [ -f /etc/bash_completion ]; then
|
|
. /etc/bash_completion
|
|
fi
|
|
fi
|
|
|
|
#
|
|
# GIT
|
|
#
|
|
[ -f $MYSCRIPTS/bash/git-completion.bash ] && . $MYSCRIPTSE/bash/git-completion.bash
|
|
|
|
#
|
|
# colored GCC warnings and errors
|
|
#
|
|
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Aliases
|
|
#
|
|
if [ $kernel == AIX ]; then
|
|
alias ls='ls -a'
|
|
alias ll='ls -la'
|
|
alias pd='pushd >/dev/null'
|
|
alias bd='popd'
|
|
else
|
|
alias ls='ls -a --color=auto'
|
|
alias ll='ls -la --color=auto'
|
|
alias pd='pushd >/dev/null'
|
|
alias bd='popd'
|
|
alias grep='grep --color=auto'
|
|
alias fgrep='fgrep --color=auto'
|
|
alias egrep='egrep --color=auto'
|
|
fi
|
|
|
|
function cgrep() {
|
|
if [[ $# -eq 0 ]]
|
|
then
|
|
echo "Usage: codegrep <pattern> "
|
|
else
|
|
grep -nR --colour \
|
|
--include=\*.cpp \
|
|
--include=\*.c \
|
|
--include=\*.hpp \
|
|
--include=\*.h \
|
|
--include=\*.inc \
|
|
--include=\*.php \
|
|
--include=\*.py \
|
|
--include=\*.sh \
|
|
--exclude-dir=.git \
|
|
--exclude-dir=.svn \
|
|
--exclude-dir=llcalc* \
|
|
--exclude-dir=00* \
|
|
"$1" .
|
|
fi
|
|
}
|
|
|
|
|
|
function create_tags() {
|
|
#Bloomberg
|
|
if [ -e /opt/swt/bin/ctags ]; then
|
|
CTAGS=/opt/swt/bin/ctags
|
|
else
|
|
CTAGS=ctags
|
|
fi
|
|
CTAGS_OPT='--recurse=yes '
|
|
CTAGS_OPT+='--verbose '
|
|
CTAGS_OPT+='--totals=yes '
|
|
CTAGS_OPT+='--tag-relative=yes '
|
|
|
|
CTAGS_DIR_CFG='.ctags_dir'
|
|
CTAGS_ROOT="$PWD"
|
|
if [ ! -f $PWD/$CTAGS_DIR_CFG ]; then
|
|
GIT_ROOT=$(git top pwd)
|
|
if [ -f $GIT_ROOT/$CTAGS_DIR_CFG ]; then
|
|
CTAGS_ROOT="$GIT_ROOT"
|
|
fi
|
|
fi
|
|
|
|
if [ -f $CTAGS_ROOT/$CTAGS_DIR_CFG ]; then
|
|
CTAGS_SRC="-L $CTAGS_ROOT/$CTAGS_DIR"
|
|
else
|
|
CTAGS_SRC="$@"
|
|
fi
|
|
|
|
cd $CTAGS_ROOT
|
|
$CTAGS $CTAGS_OPT $CTAGS_SRC
|
|
cd -
|
|
}
|
|
|
|
# Add an "alert" alias for long running commands. Use like so:
|
|
# sleep 10; alert
|
|
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
|
|
|
|
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
#
|
|
# Run local settings.
|
|
#
|
|
[ -x $HOME/.bashrc.local ] && . $HOME/.bashrc.local
|
|
|
|
#
|
|
# Clean error level.
|
|
#
|
|
[ 1 ]
|