Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing text colors in script 2

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
How do I change colors from the default shell color in my script, to say, Red and Green? I assume I will need a delimiter of some sort in a print statement.
 
If you tell me how you do it in a normal shell, I will tell you how to do it in a script.
 
well, if you want to change the color in the shell, you have to use an escape sequence:

the following simple script will show you what colors are which, in ksh:

Code:
#!/usr/bin/ksh

echo "^[[30mtest^[[0m";
echo "^[[31mtest^[[0m";
echo "^[[32mtest^[[0m";
echo "^[[33mtest^[[0m";
echo "^[[34mtest^[[0m";
echo "^[[35mtest^[[0m";
echo "^[[36mtest^[[0m";
echo "^[[37mtest^[[0m";

be careful though, that funky sequence in vi, for example, has to be done like this:

(ctrl-v)(escape)([){3)(5)(m)

the shell will not read it unless its an escape sequence.
 
Code:
#!/usr/bin/perl

use Term::ANSIColor;

print color("red"), "This is coloured red\n", color("reset");


Kind Regards
Duncan
 
Duncster to the rescue! That is the solution I would go with as well.
Twinkle twinkle little...

--jim
 
ok figured out how to side step it for the colors. My ascii escapes werent working because my secure crt config was set up wrong. I appreciate the help.
 
On a slightly unrelated note, I found this script somewhere on Red Hat's website. The script changes the prompt to different colors for different users who log in. For example, when root logs in, you get a red prompt, but when a regular user logs in, they get a purple prompt. This makes it very easy to look at a screen and easily find all the prompts where you typed commands.

Code:
#------------------------------------------------------------------------
# ISO 6429 character sequences for colors etc
# lc = leading character sequence, common for all colors.
lc='\[\e[1;'
# foregrounds----backgrounds--------------------------------------------------
BLACK=${lc}30m;  B_BLACK=${lc}40m
RED=${lc}31m;    B_RED=${lc}41m
GREEN=${lc}32m;  B_GREEN=${lc}42m
YELLOW=${lc}33m; B_YELLOW=${lc}43m
BLUE=${lc}34m;   B_BLUE=${lc}44m
PURPLE=${lc}35m; B_PURPLE=${lc}45m
CYAN=${lc}36m;   B_CYAN=${lc}46m
WHITE=${lc}37m;  B_WHITE=${lc}47m
#------------------------------------------------------------------
BRIGHT=${lc}1m
UNDER=${lc}4m
FLASH=${lc}5m
RC=${lc}0m  # reset character
SEP="\\\$"  # separator
#------------------------------------------------------------------------
if [ "x${LOGNAME}" = "x" ]
then
  LOGNAME=$(whoami)
fi

# set pc, the prompt color
if [ "$USER" = "root" ]
then
  pc=$RED
else
  pc=$PURPLE
fi
#------------------------------------------------------------------------

# set the prompt
if [ $TERM  = "dumb" ]
then
  # no color if a dumb terminal
  pc=""; RC=""
fi

PS1="${pc}\]\u@\h \W\\$ ${RC}\]"
#PS1="${pc}\][\u@\h \w]$SEP${RC} "
#------------------------------------------------------------------------

Just name this script 'colorprompt.sh' and copy it to your /etc/profile.d/ directory.


ChrisP, RHCE
 
Not to be outdone: LOL
Code:
#!/usr/local/bin/perl -w
use strict;

my $BLACK="\e[30m";
my $B_BLACK="\e[40m";
my $RED="\e[31m";
my $B_RED="\e[41m";
my $GREEN="\e[32m";
my $B_GREEN="\e[42m";
my $YELLOW="\e[33m";
my $B_YELLOW="\e[43m";
my $BLUE="\e[34m";
my $B_BLUE="\e[44m";
my $PURPLE="\e[35m";
my $B_PURPLE="\e[45m";
my $CYAN="\e[36m";
my $B_CYAN="\e[46m";
my $WHITE="\e[37m";
my $B_WHITE="\e[47m";
my $RESET_COLOR="\e[0m";
my $BLINK="\e[5m";

my $NAME = "My name is: ".$ARGV[0];
print $CYAN.$BLINK.$NAME.$RESET_COLOR." \n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top