´ÙÀ½ ÀÌÀü Â÷·Ê

8. ÇÁ·ÒÇÁÆ® »öÀ» µ¿ÀûÀ¸·Î ºÒ·¯ µéÀ̱â

8.1 ¿¹ "Proof of Concept"

¾Æ·¡ÀÇ ¿¹´Â Èï¹Ì·Î¿î ÇÁ·ÒÇÁÆ®¶ó±â º¸´Ù´Â "proof of concept" ÀÔ´Ï´Ù:µ¿ÀûÀ¸·Î ÇÁ·ÒÇÁÆ® ¾ÈÀÇ »ö±òÀ» ¹Ù²Ù´Â °ÍÀÔ´Ï´Ù. ÀÌ ¿¹¿¡¼­´Â, ½Ã½ºÅÛÀÇ ºÎÇÏ¿¡ µû¶ó¼­, È£½ºÆ® À̸§ÀÇ »ö±òÀ» ¹Ù²Ù°Ô µË´Ï´Ù(°æ°í ·Î¼­).

#!/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[0;37m\]:\w]$ "
}

¿©·¯ºÐÀÌ ¼±È£ÇϽô ÆíÁý±â¸¦ ÀÌ¿ëÇؼ­, ÀÌ ÆÄÀÏÀ» "hostloadcolour"¶ó´Â À̸§À¸·Î ÀúÀåÇϽʽÿÀ. ¸¸¾à, ¹è½¬ÇÁ·ÒÇÁÆ® ÆÐÅ°Áö¸¦ ÀÌ¹Ì ¼³Ä¡Çϼ̴ٸé, ÀÌ ÆÄÀÏÀº Å׸¶ÀÇ Çϳª·Î¼­ ÀÛµ¿ÇÒ °ÍÀÔ´Ï´Ù. ¹è½¬ÇÁ·ÒÇÁÆ®¸¦ ¼³Ä¡ÇÏÁö ¾Ê¾Ò´Ù¸é, 'source hostloadcolour'¶ó°í Ä¡°í³ª¼­, 'hostloadcolour'¸¦ ÀÔ·ÂÇϽʽÿÀ. ¾î´À ÂÊÀ̵çÁö, "prompt_command"´Â ¿©·¯ºÐµéÀÇ È¯°æ¿¡¼­ ÇÔ¼öµé Áß Çϳª°¡ µË´Ï´Ù. À§ Äڵ带 Àß »ìÆì º¸½Ã¸é, »ö±òµé($COLOUR_HIGH ¿Í $COLOUR_LOW)ÀÌ ºÎºÐÀûÀÎ »ö»ó ÄÚµå(Áï, "\[\033[1;34m\]" ´ë½Å¿¡ "1;34")·Î ¸¸ ¼³Á¤µÇ¾î ÀÖ´Ù´Â °ÍÀ» ¾Æ½Ã°Ô µÉ °ÍÀÔ´Ï´Ù. ÀÌ ¹æ¹ýÀ» Àú´Â ´õ ¼±È£ÇÕ´Ï´Ù. Àú´Â ¿Ï¼ºµÈ ÄÚµå·Î ÀÛµ¿ÇÏ°Ô ÇÒ ¼ö°¡ ¾ø¾ú´Âµ¥, ÀÌ ¹®Á¦¸¦ ´Ù·ê ¼ö ÀÖÀ¸¸é Àú¿¡°Ô ÀÏ·¯ ÁֽʽÿÀ.


´ÙÀ½ ÀÌÀü Â÷·Ê