Last active
January 25, 2016 09:24
-
-
Save ruskotron/ff8d7f848eaf519fad96 to your computer and use it in GitHub Desktop.
My .bash_profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# rrusk 2015-11-25 Universal bash profile | |
# 2015-12-21 Added Colour Output for Ant | |
# 2016-01-25 ignore CVS and .svn autocomplete | |
# notify version | |
echo hello 8.2 | |
# | |
# included using ". $BOX_HOME/scripts/my_bashrc" from ~/.bashrc | |
# | |
# The following environment variables must have already been set: | |
# export JAVA_HOME=<java home> | |
# export ANT_HOME=<ant home> | |
# export M2_HOME=<maven home> | |
# export GIT_HOME=<location of all GIT repositories> | |
# export SVN_HOME=<location of all SVN repositories> | |
# export CVS_HOME=<location of all CVS repositories> | |
# export BOX_HOME=<location of cloud storage service (dropbox etc.)> | |
# export PYTHONHOME=<location of python> | |
# | |
export EDITOR=vi | |
export FIGNORE=CVS:.svn | |
function __check { | |
if [[ x"$(eval echo \$$1)" == 'x' ]] ; then | |
echo " * ${1} is not set" >&2 | |
return 1 | |
fi | |
test -e "$1" && return 1 | |
return 0 | |
} | |
# add user/host if on a remote host | |
if [[ x${SSH_CLIENT}${REMOTE} != x ]]; then | |
uathost="\u@\h:" | |
fi | |
test "x${TERM}" = "xxterm" && export color_prompt=yes | |
# check for colour terminal support | |
if [[ $TERM =~ "color" || "$color_prompt" = yes || $OSTYPE =~ "cygwin" ]]; then | |
# cool prompt with colours | |
PS1='\e]0;\w\007\n\[\033[01;32m\]\[\033[00m\](\[\033[01;30m\]'${uathost}'\w\[\033[00m\])\n \$ ' | |
# Coloured output from Ant | |
export ANT_ARGS='-logger org.apache.tools.ant.listener.AnsiColorLogger' | |
else | |
# cool prompt with no colour | |
PS1='\n('${uathost}'\w)\n \$ ' | |
fi | |
# Python development environment | |
if __check PYTHONHOME ; then | |
export PATH="$PYTHONHOME":$PATH | |
fi | |
# Java development environment | |
if __check JAVA_HOME ; then | |
export PATH="$JAVA_HOME"/bin:"$PATH" | |
__check ANT_HOME && export PATH="$ANT_HOME"/bin:"$PATH" | |
if __check M2_HOME ; then | |
export M2="$M2_HOME"/bin | |
export PATH="$M2":$PATH | |
fi | |
fi | |
# scripts directory | |
__check BOX_HOME && export PATH=$BOX_HOME/scripts:$PATH | |
# actions specific to windows cygwin environment | |
if [[ $OSTYPE =~ "cygwin" ]]; then | |
# include windows-specific utilities: | |
__check BOX_HOME && export PATH="$BOX_HOME"/bin_win:"$PATH" | |
function __winpath { | |
while [[ x$1 != x ]]; do | |
__check $1 && eval "export $1=\"\$(cygpath -w \"\$$1\")\"" | |
shift | |
done | |
} | |
# convert to windows paths | |
__winpath JAVA_HOME ANT_HOME M2_HOME M2 PYTHONHOME | |
# This is controversial, and may not suit all situations, | |
# I understand and accept the risks but believe it to be | |
# a reasonable tradeoff. | |
# https://stackoverflow.com/questions/9292376/bash-shell-home-assignment-and-script-execution | |
# | |
# (set HOMESET to any value in calling context e.g. bashrc to disable) | |
# | |
test $HOMESET || export HOME="$(cygpath -O)" | |
# change to the new working directory | |
cd | |
fi | |
# Print a "distribution" of the values passed to input, in order of frequency. | |
alias dist="sort | uniq -c | sort -n" | |
# Another cool script. | |
alias pivot="awk -F\| -f $BOX_HOME/scripts/pivot.awk" | |
# A handy command-line timestamp-conversion tool. | |
if [[ $OSTYPE =~ "darwin" ]]; then | |
echo > /dev/null | |
# no mac implementation of Epoch conversion functions | |
else | |
# Set base of 'my' epoch (here it's the start of the 21st century). | |
export MY_EPOCH=$(expr $(date --date="2000/01/01 00:00:00" +%s)) | |
# Format date and time, as expressed in minutes from start of 'my' epoch. | |
function strftime_my { | |
date --date="@$(expr $MY_EPOCH + \( $1 \* 60 \) )" | |
} | |
# format date, as expressed in days from start of 'my' epoch. | |
function strfdate_my { | |
date --date="@$(expr $MY_EPOCH + \( $1 \* 60 \* 60 \* 24 \) )" +"%a, %b %d, %Y" | |
} | |
fi | |
function __in_all_subdirs_do { | |
basedir=$1 | |
shift | |
message=$1 | |
shift | |
echo | |
echo "$message $basedir" | |
echo | |
cd $basedir | |
for subdir in */; do ( | |
[[ $subdir = _* ]] && exit | |
cd $subdir | |
echo "-" | |
while [[ x$1 != "x" ]]; do | |
my_command=$1 | |
command_summary=$(echo $1 | awk '{print $1}') | |
shift | |
echo "$subdir \$ $my_command" | |
# $my_command 2>&1 | awk '{ print " > '$subdir' '$command_summary': " $0 }' | |
$my_command 2>&1 | awk '{ print "'$subdir' > " $0 }' | |
echo | |
if [[ ${PIPESTATUS[0]} != 0 ]]; then | |
echo " ! $subdir > Error in $my_command" | |
break; | |
fi | |
done | |
) done | |
echo "Ready[5m|[25m" | |
} | |
function test_all { | |
__in_all_subdirs_do /home/rrusk TEST pwd "ls -a" "echo hello" "ps" "!echo bad" "sleep 1" | |
} | |
# Sync all git repos and fetch all remote changes. | |
__check GIT_HOME && function begin_git { | |
__in_all_subdirs_do "$GIT_HOME" Fetch "git fetch --all" "git stash" "git merge" "git stash pop" | |
} | |
# Sync all git repos and fetch all remote changes. | |
__check CVS_HOME && function begin_cvs { | |
__in_all_subdirs_do "$CVS_HOME" Update "cvs update" | |
} | |
__check GIT_HOME && function merge_git_branches { | |
echo "git merge <active>" | |
# stash (all) working files before merging | |
git stash -a | |
git merge | |
git branch -v | { | |
while read b; do | |
branch="$(echo "${b}" | sed 's/^\* //' | awk '{print $1}')" | |
# leading '*' signals active branch, which has already been merged so skip | |
echo "${b}" | grep -q '^*' && active="${branch}" && continue | |
# skip branches that don't need to be merged | |
echo "${b}" | egrep -q '^\*? *[a-zA-Z0-9._]+ +[a-f0-9]+ +\[behind [0-9]+\]' || continue | |
echo "git checkout ${branch}" | |
git checkout "${branch}" | |
checked_out="${branch}" | |
echo "git merge" | |
git merge | |
done | |
# return to active branch, if another branch has been merged in the meantime ... | |
test "${checked_out}" && \ | |
test "${checked_out}" != "${active}" && \ | |
echo "git checkout ${active}" && \ | |
git checkout "${active}" # check out original branch | |
git stash pop # restore stashed working files | |
} | |
} | |
# Sync all git repos and fetch all remote changes. | |
__check GIT_HOME && function merge_git_all { | |
__in_all_subdirs_do $GIT_HOME Merge merge_git_branches | |
} | |
# Sync all GIT repos and fetch all remote changes. | |
__check SVN_HOME && function begin_svn { | |
__in_all_subdirs_do "$SVN_HOME" Update "svn up --force" | |
} | |
function findJar { | |
basedir=$1 | |
searchString=$2 | |
find $basedir -type f | egrep "(\.jar|\.war|\.ear)$" | while read jarFile; do | |
jar tf $jarFile | grep $searchString > /tmp/findJar.tmp | |
if [[ $? -eq 0 ]]; then | |
echo " *** $jarFile" | |
cat /tmp/findJar.tmp | |
fi | |
done | |
rm /tmp/findJar.tmp 2> /dev/null | |
} | |
function onFileChange { | |
file=$1 | |
shift | |
old_file_string=$(ls -l $file) | |
while sleep 1; do | |
file_string=$(ls -l $file) | |
if [[ x$file_string != x$old_file_string ]]; then | |
$@ | |
old_file_string=$file_string | |
echo | |
echo ............................................................................... | |
echo | |
fi | |
done | |
} | |
function hgrep { | |
n_head=1 | |
if [[ ${1} == "-n" && ${#} > 2 && $(test ${#} -ge 3 2> /dev/null) != 0 ]]; then | |
n_head=$2 | |
shift 2 | |
fi | |
search="$(echo "${@}" | sed 's|\/|\\\/|g')" | |
awk 'NR<='${n_head}' || /'"${search}"'/' | |
} | |
__check SUBLIME_HOME && function sublime { | |
"${SUBLIME_HOME}/sublime_text.exe" $@ & | |
} | |
test -f ~/local-alias.bash-env && . ~/local-alias.bash-env | |
alias ls='ls --color=auto' | |
LS_COLORS='di=1:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35:*.rpm=90' | |
export LS_COLORS | |
# determine hostname | |
test \! $HOSTNAME && export HOSTNAME=$(hostname 2>/dev/null || echo splat) | |
# and we're done! | |
echo "Ready[5m|[25m" | |
# | |
# end | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment