d3trax
3/3/2014 - 7:30 PM

functions.sh

alias ports='lsof -i -n -P' 								# view programs using an internet connection
alias portstats='sudo netstat -s' 							# show statistics for all ports

alias tcpstats='sudo netstat -st' 							# show statistics for tcp ports
alias tcp_='sudo netstat -atp' 								# list all TCP ports
alias tcp_trace='pkt_trace tcp'								# to show all TCP packets
alias udpstats='sudo netstat -su' 							# show statistics for udp ports
alias udp='sudo netstat -aup' 								# list all UDP ports
alias udp_trace='pkt_trace udp'								# to show all UDP packets

alias compiz-reset='gconftool-2 --recursive-unset /apps/compiz-1 && unity --reset'	# to reset Compiz
alias gnome-fallback-session='gsettings set org.gnome.desktop.session session-name "gnome-fallback"'	# GNOME3 desktop session
alias gnome-fallback-set='sudo /usr/lib/lightdm/lightdm-set-defaults -s gnome-fallback'			# auto login under set GNOME3 desktop
alias gnome-shell-reset='gnome-shell --replace'						# to reset Gnome Shell
alias gnome-shell-session='gsettings set org.gnome.desktop.session session-name "gnome-shell"'		# GNOME3 desktop session
alias gnome-shell-set='sudo /usr/lib/lightdm/lightdm-set-defaults -s gnome-shell'			# auto login under set GNOME3 desktop

###### shows directory tree (requires 'tree': sudo apt-get install tree)
function treecd() {
	builtin cd "${@}" &>/dev/null
	. $BSNG_RC_DIR/dirinfo/display
	dirinfo_display
	echo -e "${epink}content:"
	tree -L 1 $TREE_OPTS
	echo "$PWD" > $HOME/.lastpwd
}

###### displays a tree of the arborescence
function treefind() {
	find "$@" | sed 's/[^/]*\//|   /g;s/| *\([^| ]\)/+--- \1/'
}

function top10() {
	# copyright 2007 - 2010 Christopher Bratusek
	history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head
}


function progressbar()
# copyright 2007 - 2010 Christopher Bratusek
{
	SP_COLOUR="\e[37;44m"
	SP_WIDTH=5.5
	SP_DELAY=0.2
	SP_STRING=${2:-"'|/=\'"}
	while [ -d /proc/$1 ]
	do
		printf "$SP_COLOUR\e7  %${SP_WIDTH}s  \e8\e[01;37m" "$SP_STRING"
		sleep ${SP_DELAY:-.2}
		SP_STRING=${SP_STRING#"${SP_STRING%?}"}${SP_STRING%?}
	done
	tput sgr0
}

function svn_update() {
	local old_revision=`svn_rev $@`
	local first_update=$((${old_revision} + 1))

	svn up -q $@
	if [ $(svn_rev $@) -gt ${old_revision} ]
	then
		svn log -v -rHEAD:${first_update} $@
	else
		echo "No Changes."
	fi
}

function svnradd() { for i in $1/*;do if [ -e "$i" ];then if [ -d "$i" ];then svn add $i;svnradd $i;else svn add $i;fi; fi;done }

function findPid() { sudo /usr/sbin/lsof -t -c "$@" ; }

function findfileand() { (($# < 2)) && { echo "usage: ffa pat1 pat2 [...]" >&2; return 1; };awk "/$1/$(printf "&&/%s/" "${@:2}")"'{ print FILENAME ":" $0 }' *; }

function newest() { find ${1:-\.} -type f |xargs ls -lrt ; }

function findexec() { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \;  ; }

function randompw() {
	if [[ $2 == "!" ]]; then
		echo $(cat /dev/urandom | tr -cd '[:graph:]' | head -c ${1:-32})
	else	echo $(cat /dev/urandom | tr -cd '[:alnum:]' | head -c ${1:-32})
	fi
}

extract () {
    if [ ! -f "$1" ] ; then
      echo "'$1' is not a valid file!"
      return 1
    fi

    # Assoc. array of commands for extracting archives
    declare -A xcmd
    xcmd=(
      [.tar.bz2]="tar xvjf"
      [.tar.gz]="tar xvzf"
      [.bz2]="bunzip2"
      [.rar]="unrar x"
      [.gz]="gunzip"
      [.tar]="tar xvf"
      [.zip]="unzip"
      [.Z]="uncompress"
      [.7z]="7z x"
    )
    # extension aliases
    xcmd[.tbz2]="${xcmd[.tar.bz2]}"
    xcmd[.tgz]="${xcmd[.tar.gz]}"

    # See which extension the given file uses
    fext=""
    for i in ${!xcmd[@]}; do
      if [ $(grep -o ".\{${#i}\}$" <<< $1) == "$i" ]; then
        fext="$i"
        break
      fi
    done

    # Die if we couldn't discover what archive type it is
    if [ -z "$fext" ]; then
      echo "don't know how to extract '$1'..."
      return 1
    fi

    # Extract & cd if we can
    fbase=$(basename "$1" "$fext")
    if ${xcmd[$fext]} "$1" && [ -d "$fbase" ]; then
      cd "$fbase"
    fi
}

function zipf() { zip -r "$1".zip "$1" ; }

function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}


PS1='\[\033[0;31m\]$(date +%H:%M)\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[01;35m\]$(parse_git_branch)\[\033[00m\]\$ '

argc () {
  count=0;
  for arg in "$@"; do
          if [[ ! "$arg" =~ '-' ]]; then count=$(($count+1)); fi;
  done;
  echo $count;
}

vi () 
{
  if [[ `argc "$@"` > 1 ]]; then /usr/bin/vim $@;
  elif [ $1 = '' ]; then /usr/bin/vim;
  elif [ ! -f $1 ] || [ -w $1 ]; then /usr/bin/vim $@;
  else
    echo -n "File is readonly. Edit as root? (Y/n): "
    read -n 1 yn; echo;
    if [ "$yn" = 'n' ] || [ "$yn" = 'N' ];
        then /usr/bin/vim $*;
        else sudo /usr/bin/vim $*;
    fi
  fi
}