Bash

Adding git status to the bash prompt

by Patrick Connelly posted on October 24, 2013

After talking to a friend of mine Jeff Larkin about bash prompts, I decided to modify what he did into a single line bash prompt. By adding the following to your .bashrc you’ll get the branch your on in git (if current directory is tracked), a color denoting the status of the branch as well as the return code of the last command (if non-zero)

#
# Define some colors first: Capitals denote bold
#
red='\e[0;31m'
RED='\e[1;31m'
green='\e[0;32m'
GREEN='\e[1;32m'
yellow='\e[0;33m'
YELLOW='\e[1;33m'
blue='\e[0;34m'
BLUE='\e[1;34m'
magenta='\e[0;35m'
MAGENTA='\e[1;35m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color

# Taken from http://www.opinionatedprogrammer.com/2011/01/colorful-bash-prompt-reflecting-git-status/
function _git_prompt() {
  local git_status="`git status -unormal 2>&1`"
  if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
    if [[ "$git_status" =~ nothing\ to\ commit ]]; then
      local ansi=$GREEN
    elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
      local ansi=$RED
    else
      local ansi=$YELLOW
    fi
    if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
      branch=${BASH_REMATCH[1]}
      #test "$branch" != master || branch=' '
    else
      # Detached HEAD.  (branch=HEAD is a faster alternative.)
      branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null ||
      echo HEAD`)"
    fi
    echo -n '[\['"$ansi"'\]'"$branch"'\[\e[0m\]]'
  fi
}

function report_status() {
  RET_CODE=$?
  if [[ $RET_CODE -ne 0 ]] ; then
    echo -ne "[\[$RED\]$RET_CODE\[$NC\]]"
  fi
}

export _PS1="\[$NC\][\u@\h \W]"
export PS2="\[$NC\]> "
export PROMPT_COMMAND='_status=$(report_status);export PS1="$(_git_prompt)${_status}${_PS1}\$ ";unset _status;'

Bash Prompt


Download and launch an Elluminate session from the command line

by Patrick Connelly posted on January 07, 2013

We use Elluminate Live! for some of our meetings, and it has always bothered me that I have to launch the browser to use Elluminate especially for reoccurring meetings. So one afternoon I set out to fix this problem. I wrote the following script and have been using it for about a month now without any issues. You can set the parameters at the head of the script if you have a reoccurring meeting you want to use, or set the parameters at run time (or in an alias for multiple reoccurring meetings) if you need too.

The only prerequisite for this script is to have javaws installed an in your PATH.

#!/bin/bash

nick="NICKGOESHERE"
participant_password="PASSWORD"
leader_password="PASSWORD"
sid="SID"
output="/tmp/meeting.jnlp"
launch_on_download="true"

#############################################################################
####                   DO NOT EDIT PAST THIS POINT                       ####
#############################################################################

usage() {
  echo "usage: $0 [-n nick] [-p password] [-s sid] [-f file] [-l] [-j]"
	echo -e "\t-n The username"
	echo -e "\t-p The elluminate 'password'"
	echo -e "\t-s The elluminate 'sid'"
	echo -e "\t-l Use the leader password or the provided password is the leader password"
	echo -e "\t-f The output file [default ./meeting.jnlp]"
	echo -e "\t-j Launch the meeting file"
}

password=""
leader="false"

while getopts "hn:p:s:f:l" opt
do
	case $opt in
		n)
			echo "setting nick $OPTARG"
			nick=$OPTARG
			;;
		p)
			echo "setting password $OPTARG"
			password=$OPTARG
			;;
		l)
			echo "using leader password"
			leader="true"
			;;
		s)
			echo "setting sid $OPTARG"
			sid=$OPTARG
			;;
		f)
			echo "setting output $OPTARG"
			output=$OPTARG
			;;
		j)
			launch_on_download="true"
			;;
		h)
			usage
			exit
			;;
		?)
			usage
			exit
			;;
	esac
done

if [ ".$password" == "." ]
then
	if [ ".$leader" == ".true" ]
	then
		password=$leader_password
	else
		password=$participant_password
	fi
fi

if [ ".$output" == "." ]
then
	output_file="meeting.jnlp"
else
	output_file=$output
fi

post_data=""
url=""
curl_type=""
referer=""

if [ ".$leader" == ".true" ]
then
	#Leader needs to be a GET not a POST.  Plus some different headers
	url="https://sas.elluminate.com/site/external/launch/meeting.jnlp?sid=$sid&miuid=$password"
	curl_type="GET"
	referer="https://sas.elluminate.com/site/external/jwsdetect/meeting.jnlp?sid=$sid&miuid=$password"

	curl -s --insecure \
		-H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
		-H 'Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3' \
		-H 'Accept-Encoding:gzip,deflate,sdch' \
		-H 'Accept-Language:en-US,en;q=0.8' \
		-H 'Connection:keep-alive' \
		-H 'DNT:1' \
		-H 'Host:sas.elluminate.com' \
		-H "Referer:$referer" \
		-H 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4' \
		-X $curl_type \
		$url > $output_file
else
	post_data="username=$nick&sid=$sid&password=$password&submit.x=0&submit.y=0"
	url="https://sas.elluminate.com/site/external/launch/meeting.jnlp"
	curl_type="POST"
	referer="https://sas.elluminate.com/site/external/launch/meeting.jnlp?sid=$sid&password=$password"

	curl -s --insecure \
		-H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
		-H 'Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3' \
		-H 'Accept-Encoding:gzip,deflate,sdch' \
		-H 'Accept-Language:en-US,en;q=0.8' \
		-H 'Cache-Control:max-age=0' \
		-H 'Connection:keep-alive' \
		-H 'Content-Type:application/x-www-form-urlencoded' \
		-H 'DNT:1' \
		-H 'Host:sas.elluminate.com' \
		-H 'Origin:https://sas.elluminate.com' \
		-H "Referer:$referer" \
		-H 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4' \
		-d "$post_data" \
		-X $curl_type \
		$url > $output_file
fi


if [ ".$launch_on_download" == ".true" ]
then
	javaws $output_file
fi

Swapping two files with a quick bash function

by Patrick Connelly posted on July 03, 2012

One thing I find myself doing a lot is swapping two files in bash. I thought about making this into a bash script then I realized 1) that’s over kill and 2) not as portable as I want. So, if you add this to your .bashrc then re-source it, you’ll be able to run the command swap to switch two files

function swap() {
  TMP_NAME="TMP_$RANDOM"
  mv "$1" "/tmp/$TMP_NAME" && mv "$2" "$1" && mv "/tmp/$TMP_NAME" "$2"
}