Skip to content

Instantly share code, notes, and snippets.

@gsomoza
Created October 18, 2012 10:11
Show Gist options
  • Save gsomoza/3910863 to your computer and use it in GitHub Desktop.
Save gsomoza/3910863 to your computer and use it in GitHub Desktop.
EC2 Bash Prompt
#!/bin/bash
#Set colour prompt using the name, platform or instance id and avzone
if [ ! -f "/opt/aws/AWS-INSTID" ]; then
curl -s --fail http://169.254.169.254/latest/meta-data/instance-id > /opt/aws/AWS-INSTID
fi
export INSTID=`cat /opt/aws/AWS-INSTID`
if [ ! -f "/opt/aws/AWS-AVZONE" ]; then
curl -s --fail http://169.254.169.254/latest/meta-data/placement/availability-zone > /opt/aws/AWS-AVZONE
fi
export AVZONE=`cat /opt/aws/AWS-AVZONE`
PLATFORM=""
if [ -f "/opt/aws/AWS-PLATFORM" ]; then
PLATFORM=`cat /opt/aws/AWS-PLATFORM`
fi
NAME=""
if [ -f "/opt/aws/AWS-NAME" ]; then
NAME=`cat /opt/aws/AWS-NAME`
fi
# Define colours
RED=31
GREEN=32
ORANGE=33
BLUE=34
PURPLE=35
CYAN=36
# Use NAME and PLATFORM if defined - otherwise use the instance ID
# or the availability zone. Set the platform colour according to the
# platform.
if [[ "$NAME" == "" ]]; then
NAME=$INSTID
fi
export NAME
if [[ "$PLATFORM" != "" ]]; then
PLAT=`echo $PLATFORM | tr '[a-z]' '[A-Z]'`
case $PLAT in
TEST*|STAGE*) PLATCOL=$GREEN
NAMECOL=$CYAN
;;
UAT*|PREPROD*) PLATCOL=$ORANGE
NAMECOL=$PURPLE
;;
LIVE*|PROD*) PLATCOL=$RED
NAMECOL=$BLUE
;;
*) PLATCOL=$BLUE
NAMECOL=$GREEN
;;
esac
else
PLAT=$AVZONE
PLATCOL=$CYAN
NAMECOL=$ORANGE
fi
export PLAT
export PLATCOL
export NAMECOL
case $TERM in
vt100)
PS1="\[\033]0;[$PLAT] $NAME\007\]\[\033[0;${PLATCOL}m\][$PLAT]\[\033[0;38m\] \[\033[0;${NAMECOL}m\]$NAME\[\033[0;38m\] [\u:\W]$ "
;;
xtermc)
export TERM=xterm
PS1="\[\033]0;[$PLAT] $NAME\007\]\[\033[0;${PLATCOL}m\][$PLAT]\[\033[0;38m\] \[\033[0;${NAMECOL}m\]$NAME\[\033[0;38m\] [\u:\W]$ "
;;
xterm*)
PS1="\[\033]0;[$PLAT] $NAME\007\]\[\033[0;${PLATCOL}m\][$PLAT]\[\033[0;38m\] \[\033[0;${NAMECOL}m\]$NAME\[\033[0;38m\] [\u:\W]$ "
;;
*)
PS1="[\u@$INSTID \W]$ "
;;
esac
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
export PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment