Bash - changing a color of a substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - changing a color of a substring
# 1  
Old 02-11-2012
Bash - changing a color of a substring

Hello!

I need to write a bash script for my university classes, and I came up with an idea of a program that would test the speed of typing - there is some random text that you have to rewrite, and the script measures time, number of mistakes etc. The text would be visible on the screen all the time, and I would like to dynamically refresh it and change the color of correctly written characters to green (or to red - for those which are wrong). This would require some way of manipulating a color of a single character in a string - is something like that even possible?

I was thinking about something like inserting $(tput setaf 1)/some_character/$(tput sgr0)" on a specified position of a string - let's say I have a string "abcd", and I want one of the characters (for example, the one on the position 2 - in this case it would be c) to be green. Can something like that be done?

I will be very grateful for any suggestions.
PS. Sorry for my English, if there are any mistakes.
# 2  
Old 02-11-2012
Unless 'tput' is a shell built-in (it's not in my environment), I'd avoid it as that's an awful lot of process creation. I'd use escape sequences assuming you're working in an xterm environment. This works under xterm run by either Kshell or bash and should illustrate how to change colours using escape sequences:


Code:
#!/usr/bin/env ksh
grey=30
red=31
green=32
yellow=33
blue=34
purple=35
white=36

function colour_string
{
        typeset colour=$1
        shift
        printf  "\e[1;%dm%s" $colour "$*"   set the desired colour with escape, then write string
}

echo "type these characters without spaces:"
desired=( a b c d e f g)
echo "${desired[@]}"
i=0
while read -n 1 x
do
        resp[$i]=$x
        i=$(( i + 1 ))
        if (( i >= ${#desired[@]} ))
        then
                break;
        fi
done

printf "\n"
for (( i = 0; i < ${#desired[@]}; i++ ))
do
        if [[ ${resp[$i]} == ${desired[$i]} ]]
        then
                colour_string $green "${resp[$i]}"
        else
                colour_string $red "${resp[$i]}"
        fi
done
printf "\n"


Last edited by agama; 02-11-2012 at 05:01 PM.. Reason: wording
# 3  
Old 02-11-2012
Or for example, just put the sequences in variables and use those in the format part of printf.
Code:
$ BLUE="\e[0;34m"
$ GREEN="\e[0;32m"
$ RED="\e[0;31m"
$ NOCOLOR="\e[0m"
$ printf "%s $BLUE%s $RED%s $GREEN%s $NOCOLOR\n" HELLO BLUE RED GREEN
HELLO BLUE RED GREEN

ANSI escape sequences:

Last edited by Scrutinizer; 04-20-2012 at 01:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-11-2012
Thanks a lot, the ANSI escape sequences list was exactly what I needed, I didn't know about that until now.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

color in bash

I have some tcsh scripts that produce output in color, but does not work in bash. Any idea on a solution? echo " \033 (6 Replies)
Discussion started by: kristinu
6 Replies

2. Programming

changing text color in c

I'm writing my own Unix ls command in c and I was wondering if anyone knows how to or can point me to a tutorial that shows me how to change the text color of the filename depending on if it's a directory, regular file, linked file, etc..., like the real ls command does? Thanks. (4 Replies)
Discussion started by: snag49ers
4 Replies

3. OS X (Apple)

Adding Color to bash

Hey everyone, I have come across an issue to where I am trying to create a script which changes the text color with a simple if then statement. I have seen it done with Fedora 8 but when I try and create it using my MacBook Pro running Snow Leopard it doesn't work. Funny thing is, when I use... (2 Replies)
Discussion started by: dachadster13
2 Replies

4. Shell Programming and Scripting

Python- Changing background color on Button click

Hi, I am trying to write a python program which changes background color on click of button. However i am stuck up. Instead of changing the color currently it is creating a new frame every time. please look at the code and let me know how to correct it #!/usr/bin/env python from Tkinter... (0 Replies)
Discussion started by: vickylife
0 Replies

5. UNIX for Advanced & Expert Users

Changing text color in existing xterm or dtterm

On solaris and irix systems, I'm using csh in an existing xterm or dterm and would like to change the text colors. How do I accomplish this? Thanks (1 Reply)
Discussion started by: fjc
1 Replies

6. Shell Programming and Scripting

How to change prompt color when changing path

Hi all, Can you tell me how to change the prompt color (only the path part) when I chnange directory with "cd"? I use the sequence below in ".bashrc" (Solaris 8) to change my prompt colors and I'd like to modify it to change the path color when I cange directory. PSC() { echo -ne "\"; }... (0 Replies)
Discussion started by: majormark
0 Replies

7. UNIX for Advanced & Expert Users

Changing Text Color Using VT100

Hi, I want to change the color of the text. Currently, I am using the following VT100 command, which changes the color of the foreground: <ESC>[{attr1};...;{attrn}m The problem is, when I change the color of the foreground, it changes the color of the text as expected, but it also... (4 Replies)
Discussion started by: Abed Alnaif
4 Replies

8. Shell Programming and Scripting

Changing font and color in log file of Script

Hi, We have process log with user defined error messages and information echos. I wanted to highlight error messgaes in that log file while writing in it. Is there any option with either echo or any other command which enables making text bold or we can change the font of body text of echo. ... (4 Replies)
Discussion started by: satgo
4 Replies
Login or Register to Ask a Question