LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2010, 02:44 AM   #1
Fliggerty
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
Changing color of output from bash script


I am writing a bash script that utilizes the output of another script (which I will refer to as script#2.) Script#2 is not owned by me, I cannot modify it. All of the output from script#2 is blue, which makes it difficult for me to read.

I would like to have the output of it changed to grey. Is there a way I can do that in my script? A command I can pipe the output to?

Edit: One other question related to this. I put a trap function in my script that works well. Script#2 essentially runs a tail -f. When I ctrl+c to stop it, it stops script#2 and never calls the trap in my script. Is there any way I can work around that?

Thanks!

Last edited by Fliggerty; 04-22-2010 at 03:06 AM.
 
Old 04-22-2010, 07:44 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Chances are the colors come from embedded ansi escape sequences. If so, you could fairly easily use sed to change or remove them. Try piping the script through cat -v to see what non-printing characters exist in the output. For example, if I run ls through it with colors on, I get:

Code:
$ ls --color|cat -v

^[[0m^[[01;32mscript1.sh^[[0m
^[[01;32mscript2.sh^[[0m
file.txt
^[[01;34mtest^[[0m
^[[01;34mvarious_scripts^[[0m
"^[[01;34m", for example, is the escape sequence for bright blue. The ^[ at the beginning is a graphical representation of a literal ascii escape character, (in the script code it will probably be represented as \e). The numbers between the second [ and the m are the modes that set the display colors. Multiple modes are strung together with semicolons. This page lists the possible codes. You'll want the "set graphics mode" section.

So to change the blue to red, for example, simply change the number with sed.
Code:
ls --color|sed 's/;34/;31/g'
To remove all the color codes, you can use an expression like this (I found this on the net, so it may not remove every code possible, but it should work for most of the common ones). Note that the escape has to be matched using the hex value.
Code:
sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"
Hopefully this will help you.

Regarding your second question, I think we'd need to see the actual code to figure out what's going on.
 
1 members found this post helpful.
Old 04-22-2010, 12:25 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Everything you want to know and more:

http://wiki.archlinux.org/index.php/Color_Bash_Prompt
 
1 members found this post helpful.
Old 04-24-2010, 08:40 AM   #4
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
+1 for arch wiki.
 
0 members found this post helpful.
  


Reply



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
Changing Bash Color Prompt carlosinfl Red Hat 2 03-25-2010 01:16 AM
I want my script output in bold and color fonts gdan2000 Linux - General 6 03-16-2009 06:29 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
[bash] changing the color set MD3 Linux - Software 2 04-27-2005 12:38 PM
Changing the color of ls output? exitsfunnel Linux - Newbie 1 02-19-2004 11:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 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