LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-01-2009, 04:49 AM   #1
trace_effect
LQ Newbie
 
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4

Rep: Reputation: 0
Adding color to a shell script?


Is it possible to add color to a shell script so that when a user runs the script, the text is in color

eg:

#!/usr/bin/sh
# game1.sh: Asks user to guess a number 5 times
# Answer is 12 and max tries is 5
count=0 ; counter=0
while ((count < 6 )) ; do
echo "Enjoy my first game!"
echo "Go ahead and guess a number, or hit ctrl c to exit:" 1>&2
read number
if [ $number -eq 8 ] ; then
echo "Congradulations, you guessed the number and get a cookie" ; exit
else
let counter=1
let count=1

echo "Sorry that was incorrect, you have 4 guesses left. Guess another number or press ctrl c to exit:" 1>&2
read number
if [$number -eq 8] ; then
echo "Congradulations, you guessed the number and get a cookie" ; exit
else
let count=2
let counter=2

echo "Sorry that was incorrect, you have 3 guesses left. Guess another number or press ctrl c to exit:" 1>&2
read number
if [$number -eq 8] ; then
echo "Congradulations, you guessed the number and get a cookie" ; exit
else
let count=3
let counter=3

echo "Sorry that was incorrect, you have 2 guesses left. Guess another number or press ctrl c to exit:" 1>&2
if $number -eq 8] ; then
echo "Congradulations, you guessed the number and get a cookie" ; exit
else
let count=4
let counter=4

echo "Sorry that was incorrect, you have 1 guess left. Guess another number or press ctrl c to exit:" 1>&2
if $number -eq 8] ; then
echo "Congradulations, you guessed the number and get a cookie" ; exit
else
let count=5
let counter=5
echo "Thanks for playing." 1>&2
if [ $count -eq $counter ] ; then
break
echo "Play again soon!"
fi
fi
fi
fi
fi
fi
done
 
Old 12-01-2009, 04:51 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Google around for "bash ansi color codes", for example:

http://tldp.org/LDP/abs/html/colorizing.html
 
Old 12-01-2009, 05:00 AM   #3
johngreenwood
Member
 
Registered: Nov 2006
Location: Lancashire, United Kingdom
Distribution: Slackware 13
Posts: 243

Rep: Reputation: 31
This link discusses using colours in a script. There is a script somewhere which generates a table of the ANSI colours but I can't find it right now, I'll have a look.

EDIT:
I found the script I mentioned, it's quite useful for knowing which code corresponds to which colour.
Code:
#!/bin/bash
#
#   This file echoes a bunch of color codes to the 
#   terminal to demonstrate what's available.  Each 
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a 
#   test use of that color on all nine background 
#   colors (default + 8 escapes).
#

T='gYw'   # The test text

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

for FGs in '    m' '   1m' '  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  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo
Found on this page.

Last edited by johngreenwood; 12-01-2009 at 05:11 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new users via Shell script coolfrog Linux - General 10 12-05-2010 10:47 AM
Shell Script - color output would not work on debian solo9300 Linux - General 2 10-20-2009 05:07 AM
Open shell script in color say_hi_ravi Linux - Newbie 3 08-31-2009 12:46 PM
Shell script adding autostart gnome script Coolrunr Programming 3 01-01-2009 02:23 PM
Adding a shell script to startup Dakkar SUSE / openSUSE 3 10-15-2005 08:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration