Register
It is currently Sat Apr 19, 2014 8:33 am

Using colors in shell scripting


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Oct 17, 2011 8:51 am   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
The last script posted referenced colors and brought to my remembrance an old script I had written to display some common colors in a terminal so I could remember what number each color was assigned. I used to use it frequently when I wanted to add some color to my scripts output. Enjoy!

Code:
#!/bin/bash

colors=(black:30 red:31 green:32 yellow:33 blue:34 purple:35 lb:36 white:37)
x=(b:40 r:41 g:42 y:43 blue:44 p:45 lb:46 w:47)

c=0
while [ $c -le 8 ]
do
        firstcolor=`echo ${colors[$c]} |awk -F':' '{print $1}'`
        firstnumber=`echo ${colors[$c]} |awk -F':' '{print $2}'`

        cc=0

        while [ $cc -le 8 ]
        do
                secondcolor=`echo ${x[$cc]} |awk -F':' '{print $1}'`
                secondnumber=`echo ${x[$cc]} |awk -F':' '{print $2}'`

                declare ${firstcolor}${secondcolor}=$'\e['${firstnumber}';'${secondnumber}'m'

                cc=`expr $cc + 1`
        done

        c=`expr $c + 1`
done

bold=$'\e[1m'
underline=$'\e[4m'
off=$'\e[0m'

clear
echo ""
echo "             $underline$bold$yellowb J$greenb E$blueb R$redb R$lbb O$purpleb D$whiteb ' S   $greenb C O L O R   $yellowb C H A R T $off"
echo ""

echo "  40      41      42      43      44      45      46      47"
echo ""
echo "30  $blackb Normal $blackr Normal $blacky Normal $blackg Normal $blackblue Normal $blackp Normal $blacklb Normal $blackw Normal $off  "
echo "    $bold$blackb Bold   $blackr   Bold $blacky   Bold $blackg   Bold $blackblue   Bold $blackp   Bold $blacklb   Bold $blackw   Bold $off"
echo "31  $redb Normal $redr Normal $redy Normal $redg Normal $redblue Normal $redp Normal $redlb Normal $redw Normal $off  "
echo "    $bold$redb Bold   $redr   Bold $redy   Bold $blackg   Bold $redblue   Bold $redp   Bold $redlb   Bold $redw   Bold $off"
echo "32  $greenb Normal $greenr Normal $greeny Normal $greeng Normal $greenblue Normal $greenp Normal $greenlb Normal $greenw Normal $off  "
echo "    $bold$greenb Bold   $greenr   Bold $blacky   Bold $greeng   Bold $greenblue   Bold $greenp   Bold $greenlb   Bold $greenw   Bold $off"
echo "33  $yellowb Normal $yellowr Normal $yellowy Normal $yellowg Normal $yellowblue Normal $yellowp Normal $yellowlb Normal $yelloww Normal $off  "
echo "    $bold$yellowb Bold   $yellowr   Bold $blacky   Bold $yellowg   Bold $yellowblue   Bold $yellowp   Bold $yellowlb   Bold $yelloww   Bold $off"
echo "34  $blueb Normal $bluer Normal $bluey Normal $blueg Normal $blueblue Normal $bluep Normal $bluelb Normal $bluew Normal $off  "
echo "    $bold$blueb Bold   $bluer   Bold $blacky   Bold $blueg   Bold $blueblue   Bold $bluep   Bold $bluelb   Bold $bluew   Bold $off"
echo "35  $purpleb Normal $purpler Normal $purpley Normal $purpleg Normal $purpleblue Normal $purplep Normal $purplelb Normal $purplew Normal $off  "
echo "    $bold$purpleb Bold   $purpler   Bold $blacky   Bold $purpleg   Bold $purpleblue   Bold $purplep   Bold $purplelb   Bold $purplew   Bold $off"
echo "36  $lbb Normal $lbr Normal $lby Normal $lbg Normal $lbblue Normal $lbp Normal $lblb Normal $lbw Normal $off  "
echo "    $bold$lbb Bold   $lbr   Bold $blacky   Bold $lbg   Bold $lbblue   Bold $lbp   Bold $lblb   Bold $lbw   Bold $off"
echo "37  $whiteb Normal $whiter Normal $whitey Normal $whiteg Normal $whiteblue Normal $whitep Normal $whitelb Normal $whitew Normal $off  "
echo "    $bold$whiteb Bold   $whiter   Bold $blacky   Bold $whiteg   Bold $whiteblue   Bold $whitep   Bold $whitelb   Bold $whitew   Bold $off"
echo ""
echo ""


Top
 Profile  
 PostPosted: Fri Oct 21, 2011 3:27 am   

Joined: Fri Oct 21, 2011 3:18 am
Posts: 2
Nice one!
If you only want to display the colors (without declaring the color variables), here is an alternative script:
Code:
#!/bin/bash

n='normal'
b=' bold '

echo -e "\n            40m        41m        42m        43m        44m        45m        46m        47m";

for FGs in '  30m' '1;30m' '  31m' '1;31m' '  32m' '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' '  36m' '1;36m' '  37m' '1;37m'; do
        FG=${FGs// /}
        echo -en " $FGs \033[$FG "
        for BG in 40m 41m 42m 43m 44m 45m 46m 47m; do
                if [[ $FG = "1;"* ]]; then
                        echo -en " \033[$FG\033[$BG  $b  \033[0m"
                else
                        echo -en " \033[$FG\033[$BG  $n  \033[0m"
                fi
        done
        echo
done
echo


Top
 Profile  
 PostPosted: Fri Oct 21, 2011 3:52 pm   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
Nice update. I didn't even think about not declaring the variables. Much quicker to load up your way and much less code.


Top
 Profile  
 PostPosted: Sun Oct 23, 2011 4:23 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
I also recoded this script (which comes from here) in pure bash(1) in the past.

Code:
#!/bin/bash                                                                                                                   
txt=\>:O

fg=({1,}m {30..37}m 1\;{30..37}m)
bg=({40..47}m)

printf '\n%17s' ' '
for _ in {40..47}; do
    printf '%s%6s' "${_/42/$'\e[1;32;40m42\e[00m'}" ' '
done
printf '\n'

for f_c in "${fg[@]}"; do
    printf ' %5s\e[%s   %s  ' $f_c $f_c "$txt"
   
    for b_c in "${bg[@]}"; do
        printf ' \e[%s\e[%s  %s  \e[00m' $f_c  $b_c "$txt"
    done
    printf '\n'
done
printf '\n'


Top
 Profile  
 PostPosted: Wed Nov 30, 2011 9:58 am   

Joined: Fri Oct 21, 2011 3:18 am
Posts: 2
jsz wrote:
I also recoded this script (which comes from here) in pure bash(1) in the past.

Code:
txt=\>:O

Calm down...I've never said it was my script. I just edited it for my purposes and use it on a regular basis to determine terminal colors for my bash scripts.
Unfortunately I didn't know the original source of this script anymore (it was on my hdd for several years, now), otherwise I had linked to it in my post.


Top
 Profile  
 PostPosted: Sat Dec 17, 2011 7:58 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
I use colors alot in many of my scripts... you can also look into "tput" for using colors in scripts. Tput is cool because not only can you control colors, you can also control placement of your text, background and forground colors... it's pretty cool.

http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html if your ever interested in learning more.

Thanks for sharing, wasn't sure alot of other people used colors in their shell scripts :)


Top
 Profile WWW  
 PostPosted: Wed Mar 14, 2012 4:25 am   

Joined: Mon Sep 26, 2011 10:17 pm
Posts: 4
Here is a script that I wrote a while ago, just to mess around with values... You can run this on your machine and test the color capability of your terminal. You may have to back-scroll a little, but not much. I am using GNOME Terminal 2.30.x
BSD-style license, so have fun & go wild.

Code:
#!/bin/bash
# ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# colortest (tests the colors of your terminal using ANSI codes)
#
# written by: Josh D. Dotson, Oct 9th, 2011
# License as follows:
#    Copyright (c) 2011 Josh D. Dotson
#
# YOU ARE HEREBY GRANTED PERMISSION TO USE THIS HOW YOU SEE FIT, AS PUBLIC DOMAIN.
# IF YOUR COUNTRY, STATE OR TOWNSHIP DOES NOT PERMIT SUCH, IT IS LICENSED SIMILAR TO
# THE BERKLEY COPYRIGHT (SUBSTITUTE ALL REFERENCES TO BERKLEY UNIVERSITY WITH "AUTHOR").
# THE DISCLAIMER IS APPLICABLE EITHER WAY, USING THE ABOVE SENTENCE
# REGARDING SUBSTITUTION OF TEXT.
#
# BERKLEY COPYRIGHT:
# * Copyright (c) 1982, 1986, 1990, 1991, 1993
# *    The Regents of the University of California.  All rights reserved.
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions
# * are met:
# * 1. Redistributions of source code must retain the above copyright
# *    notice, this list of conditions and the following disclaimer.
# * 2. Redistributions in binary form must reproduce the above copyright
# *    notice, this list of conditions and the following disclaimer in the
# *    documentation and/or other materials provided with the distribution.
# * 3. All advertising materials mentioning features or use of this software
# *    must display the following acknowledgement:
# *   This product includes software developed by the University of
# *   California, Berkeley and its contributors.
# * 4. Neither the name of the University nor the names of its contributors
# *    may be used to endorse or promote products derived from this software
# *    without specific prior written permission.
# *
# * DISCLAIMER:
# * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# * SUCH DAMAGE.
# ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
#
# HOW TO USE:
#   ./colortest (optional character to use instead of "O")
#
# Purpose:
#   To show you if colors are available on your terminal, and what they look like.
#   
# ANSI code format:
#   echo -ne "\033[00;38;5;007m"
#
#  the "n" in "-ne" tells bash not to put a newline at the end of the echo
#  the "e" in "-ne" tells bash interpret the escape sequences
#  the "\033" in the code is the escape character
#  the fourth group (007) is the color index for the palate
#
#  Maybe someone with a higher understanding of the code could correct this?
#  The values seem to be added together somehow, but I am too lazy to find it...

m="m"
br_a=8
br_b=$(($br_a + 8))
br_c=$(($br_b + 36)); br_d=$(($br_c + 36)); br_e=$(($br_d + 36))
br_f=$(($br_e + 36)); br_g=$(($br_f + 36)); br_h=$(($br_g + 36))
c_dim=2; c_norm=0; c_bold=1; c_inverse=7; c_undl=4; c_strk=9; norm='\033[0m'
y=8; x=5

function alpha(){
      for t in 0 1 2; do
         for u in 0 1 2 3 4 5 6 7 8 9; do
            for v in 0 1 2 3 4 5 6 7 8 9; do
               if [ $t$u$v -eq $br_a ];then echo ""; fi  # These if's break the line
               if [ $t$u$v -eq $br_b ];then echo ""; fi  # in a desired place to show
#               if [ $t$u$v -eq $br_c ];then echo ""; fi  # the association of the colors.
#               if [ $t$u$v -eq $br_d ];then echo ""; fi  # Feel free to uncomment
               if [ $t$u$v -eq $br_e ];then echo ""; fi  # more if's.
#               if [ $t$u$v -eq $br_f ];then echo ""; fi
#               if [ $t$u$v -eq $br_g ];then echo ""; fi
               if [ $t$u$v -eq $br_h ];then echo ""; fi
               if [ $t$u$v -lt 256 ];then
                     echo -ne "\033[0$z;3$y;$x;$t$u$v$m$char$norm"
               fi
            done
         done
      done
echo ""
}

echo -e "line 1=regular (8) [+0]\nline 2=bright (8) [+8]\nline 3/4=colorscale (216) [+16]\nlast line=grayscale (24) [+232]\n"
if [ "x$1" == "x" ];then char="O"; else char="$1"; fi

#set the attribute; echo the selected char using attribute; display code used; then print table using attribute
z=$c_dim; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Dim;      \033[0$z;3$y;$x;007$m"; alpha; echo ""
z=$c_norm; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Normal;      \033[0$z;3$y;$x;007$m"; alpha; echo ""
z=$c_bold; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Bold;      \033[0$z;3$y;$x;007$m"; alpha; echo ""
z=$c_inverse; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Inverse;      \033[0$z;3$y;$x;007$m"; alpha; echo ""
z=$c_undl; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Underline;      \033[0$z;3$y;$x;007$m"; alpha; echo ""
z=$c_strk; echo -ne "\033[0$z;3$y;$x;007$m$char$norm "; echo "Attribute: Strike-through;   \033[0$z;3$y;$x;007$m"; alpha; echo ""


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP