LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-23-2016, 10:40 AM   #1
Zethien
LQ Newbie
 
Registered: Nov 2016
Posts: 1

Rep: Reputation: Disabled
Wink Quick question with color in bash script


This isn't the biggest deal in the world but when making a shell script to modify the output a bit from the ping command, I use red and green color for up or down host status via /e[0;xxm.

It works well for displaying in the shell but when I >> to a file I don't see the color I see the ^[[0;31m and not the color.

Anything I can do to fix that lil headache?

I'm invoking color in the script via a variable RED='\e[0;xxm' and calling it in my echo as ${RED}.

Last edited by Zethien; 11-23-2016 at 10:41 AM. Reason: syntax errors
 
Old 11-23-2016, 11:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,842
Blog Entries: 13

Rep: Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894
Member Response

I think this depends a bit on the file. For instance I do not believe a simple text file would display any colors; however there are various document format types, such as ODT or HTML, maybe XML(?), where you can add color attributes and be able to see those when you load the document in the appropriate reader.

What types of files are you presently using?
 
Old 11-23-2016, 11:43 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,075

Rep: Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245Reputation: 1245
The color sequences only have meaning to a color terminal. Ctrl-[ and '\e' are both names for the same ASCII value 27. I suspect if you cat the file it will show red. You are seeing ^[[0;31m when you look at the file with an editor or some other program that doesn't know how to interpret the color sequence.
 
Old 11-26-2016, 05:22 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
if you do a "cat file" in a terminal, you should see the colors again.

ultimately you have to ask yourself, what is the goal of the script? where should its output show up? maybe you have to use html or pango instead of shell color escape sequences.
 
Old 11-26-2016, 05:53 AM   #5
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,172

Rep: Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504Reputation: 2504
Quote:
Originally Posted by Zethien View Post
This isn't the biggest deal in the world but when making a shell script to modify the output a bit from the ping command, I use red and green color for up or down host status via /e[0;xxm.

It works well for displaying in the shell but when I >> to a file I don't see the color I see the ^[[0;31m and not the color.

Anything I can do to fix that lil headache?

I'm invoking color in the script via a variable RED='\e[0;xxm' and calling it in my echo as ${RED}.
That is not a headache, that is to be expected.
To expand upon what others have already said: bash does not do colors, (some) terminals do colors. When you use bash to echo screen control codes appropriate for the terminal type you can display colors, but it is the terminal that is converting the codes into colors. Pipe that to a file and look at the file and you will see the text (including codes) that generated the colors because they are not being sent to the terminal in a way that it will interpret.

My personal solution: you can output html with color codes so that any browser will interpret the colors, but then you must be prepared to look at the output using a browser. VI or any other text editor will see the html text, but any browser will display with color, formatting, etc. (I generate my web pages this way, using scripts that output the html and css.)

I question if that would be worth your time.
Rather than modifying the output of the ping, why not detect the results of the ping and output your own "success" or "FAIL" text (colored or not) to simplify the problem. If up or down status is all you need, all of the other ping verbiage is wasted anyway.
 
  


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
[SOLVED] Quick Help With Bash Script jlacroix Programming 20 04-12-2012 04:24 PM
Changing color of output from bash script Fliggerty Programming 3 04-24-2010 09:40 AM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 01:12 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
really quick bash question fibbi Linux - Software 3 06-15-2004 11:14 PM

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

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