Created
March 9, 2018 22:02
-
-
Save tyzbit/42aa3ec6ba07146cbb26cd7dfd542595 to your computer and use it in GitHub Desktop.
Bash Prompt Timestamp and colored kubectl info
This file contains hidden or 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
| export UPDATE_TIME=0 | |
| promhostname=$(hostname | cut -d'.' -f1) | |
| #grab the 8 characters after the first part of the hostname (ABC-lx00000000) | |
| hostname=$(hostname|cut -c9-15|cut -d'.' -f1) | |
| #if the hostname is less than 8 characters, pad it with dashes until it's 8 characters | |
| i=0;while [ $(echo $hostname | wc -m) -le 8 ]; do | |
| [ $i = 0 ] && hostname=$(echo $hostname"-") || hostname=$(echo "-"$hostname); i=$((1-i)) | |
| done | |
| export PS1='\[\033[01;35m\][$hostname] \[\033[0m\]\w$ ' | |
| extrapsone() { | |
| context=$(kubectl config current-context) | |
| if [[ "$context" =~ "dev" ]]; then | |
| color='\033[01;32m' | |
| elif [[ "$context" =~ "stag" ]]; then | |
| color='\033[01;33m' | |
| else | |
| color='\033[01;31m' | |
| fi | |
| if [ "$KUBE" = 1 ]; then | |
| export PS1='\[\033[01;35m\][$hostname] \[\033[0m\]\w ('$color'$context\[\033[0m\])$ ' | |
| else | |
| export PS1='\[\033[01;35m\][$hostname] \[\033[0m\]\w$ ' | |
| fi | |
| } | |
| PROMPT_COMMAND='history -a; printf "\033]0;$promhostname\a"; extrapsone; export UPDATE_TIME=1' | |
| #add trap to put current time into new commands | |
| trap 'if [ $UPDATE_TIME -eq 1 ]; then printf "\033[1A\033[01;35m[$(/bin/date +"%R:%S")]\033[0m\n"; export UPDATE_TIME=0; fi' DEBUG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment