Monday, October 27, 2008

Give Your Shell Some Colours

Came across this Colorful Shells -- Using ANSI Color Codes. I think you can start putting colours in your shell scripting.

Instead of using captial letter to draw someone's attention, you can use red (or even blink) to show error messages and green to show succes:

red="\033[31m"
green="\033[32m"
off="\033[0m"
echo -e "${red}Error. Unable to run command${off}"
echo -e "${green}OK. Run successful.${off}"

If you are going to run the script in above mentioned article (show below), you will find out the effects of the combination of all the sequences.

for attr in 0 1 4 5 7 ; do
  echo "----------------------------------------------------------------"
  printf "ESC[%s;Foreground;Background - \n" $attr
  for fore in 30 31 32 33 34 35 36 37; do
    for back in 40 41 42 43 44 45 46 47; do
      printf ’\033[%s;%s;%sm %02s;%02s ’ $attr $fore $back $fore $back
    done
    printf ’\n’
  done
  printf ’\033[0m’
done

Here is the summary of the sequence:

  • Text properties - 0(default), 1(bold), 22(not bold), 4(underlined), 24(not underlined), 5(blinking), 25(not blinking), 7(invers), 27(not invers)
  • Foreground colour - 30(black), 31(red), 32(green), 33(yellow), 34(blue), 35(magenta), 36(cyan), 37(white)
  • Background colur - 40(black), 41(red), 42(green), 43(yellow), 44(blue), 45(magenta), 46(cyan), 47(white)

Labels:

0 Comments:

Post a Comment

<< Home