<HTML ><HEAD ><TITLE >A "Proof of Concept" Example</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Loading Prompt Colours Dynamically" HREF="c670.html"><LINK REL="PREVIOUS" TITLE="Loading Prompt Colours Dynamically" HREF="c670.html"><LINK REL="NEXT" TITLE="Prompt Code Snippets" HREF="c679.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c670.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 10. Loading Prompt Colours Dynamically</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c679.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN672" ></A >10.1. A "Proof of Concept" Example</H1 ><P >This is a "proof of concept" more than an attractive prompt: changing colours within the prompt dynamically. In this example, the colour of the host name changes depending on the load (as a warning).</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >#!/bin/bash # "hostloadcolour" - 17 October 98, by Giles # # The idea here is to change the colour of the host name in the prompt, # depending on a threshold load value. # THRESHOLD_LOAD is the value of the one minute load (multiplied # by one hundred) at which you want # the prompt to change from COLOUR_LOW to COLOUR_HIGH THRESHOLD_LOAD=200 COLOUR_LOW='1;34' # light blue COLOUR_HIGH='1;31' # light red function prompt_command { ONE=$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/" -e "s/ //g") # Apparently, "scale" in bc doesn't apply to multiplication, but does # apply to division. ONEHUNDRED=$(echo -e "scale=0 \n $ONE/0.01 \nquit \n" | bc) if [ $ONEHUNDRED -gt $THRESHOLD_LOAD ] then HOST_COLOUR=$COLOUR_HIGH # Light Red else HOST_COLOUR=$COLOUR_LOW # Light Blue fi } function hostloadcolour { PROMPT_COMMAND=prompt_command PS1="[$(date +%H%M)][\u@\[\033[\$(echo -n \$HOST_COLOUR)m\]\h\[\033[0m\]:\w]$ " }</PRE ></FONT ></TD ></TR ></TABLE ><P >Using your favorite editor, save this to a file named "hostloadcolour". If you have the Bashprompt package installed, this will work as a theme. If you don't, type <TT CLASS="USERINPUT" ><B >source hostloadcolour</B ></TT > and then <TT CLASS="USERINPUT" ><B >hostloadcolour</B ></TT >. Either way, "prompt_command" becomes a function in your environment. If you examine the code, you will notice that the colours ($COLOUR_HIGH and $COLOUR_LOW) are set using only a partial colour code, ie. "1;34" instead of "\[\033[1;34m\]", which I would have preferred. I have been unable to get it to work with the complete code. Please let me know if you manage this.</P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c670.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="c679.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Loading Prompt Colours Dynamically</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c670.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Prompt Code Snippets</TD ></TR ></TABLE ></DIV ></BODY ></HTML >