s i s t e m a o p e r a c i o n a l m a g n u x l i n u x | ~/ · documentação · suporte · sobre |
Next
Previous
Contents
10. Loading Prompt Colours Dynamically10.1 A "Proof of Concept" ExampleThis 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).
#!/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]$ " }
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
Next Previous Contents |