Identify Color and send email with same color


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify Color and send email with same color
# 1  
Old 03-22-2014
Identify Color and send email with same color

Hello mates,

I have a requirement where in which I have to mail an output from multiple programs in the same colour as the output from shell script.

I have seen a post to mail html in our forum,but my case is I have to first identify which colour the output is in an then mail it to in the same color ,guys please help me this question has become more of a conundrum to me Smilie.


Regards
Kingcobra

---------- Post updated 03-23-14 at 12:11 AM ---------- Previous update was 03-22-14 at 09:37 PM ----------
Code:
ls -lrt>king.txt;txt2html king.txt
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta name="generator" content="HTML::TextToHTML v2.51"/>
</head>
<body>
<p>total 88<br/>
-rw-r--r-- 1 mulpuri mulpuri 8445 Feb 24 00:16 examples.desktop
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Videos
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Templates
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Public
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Pictures
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Music
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Documents
drwxr-xr-x 6 mulpuri mulpuri 4096 Mar 11 21:27 Downloads
drwxrwxr-x 6 mulpuri mulpuri 4096 Mar 22 12:07 workspace
-rw-rw-r-- 1 mulpuri mulpuri 1988 Mar 22 13:07 kart.txt
drwxr-xr-x 2 mulpuri mulpuri 4096 Mar 22 21:27 Desktop
-rw-rw-r-- 1 mulpuri mulpuri 681 Mar 23 00:06 king
-rw-rw-r-- 1 mulpuri mulpuri 0 Mar 23 00:06 king.txt</p>

</body>
</html>

I can convert the text to html,by where I am struck is that I need to retain the color;all of us are aware that with default settings in ubuntu directories will be in blue color however when I convert and mail the content taht will be plain white;please help me finding the answer to this question

Last edited by Scott; 03-22-2014 at 02:47 PM.. Reason: Please use code tags
# 2  
Old 03-23-2014
If I am reading your post correctly then you are looking for the attributes to a string/character at any random part of an echo to a terminal.
This can't be done so you have no choice but to do it manually.
The terminal emulator has no concept of BIOS interrupt (0x)10, subcommand (0x)08, IIRC, so you will have to hard code each line of your listing yourself.

I am not sure how much easier it would be to hard code each complete line listing in the corresponding colour(s) but could be an alternative to look at something like this...
Code:
-rw-r--r-- 1 mulpuri mulpuri 8445 Feb 24 00:16 examples.desktop
drwxr-xr-x 2 mulpuri mulpuri 4096 Feb 24 00:19 Videos

This User Gave Thanks to wisecracker For This Post:
# 3  
Old 03-23-2014
Thanks wisecracker for your reply,the actual requirement in our project is that we have a java program that prints text to the terminal when manually run and red when printed in all instances is an error,our requirement is that we need to send an email whenever red is outputted.
Now this check is being done manually we are planning to convert to cron tab and divert this output to a file and mail in cases only when file has anything in red color.But the problem here is when I redirect I am lossing the colors

---------- Post updated at 08:12 PM ---------- Previous update was at 08:10 PM ----------

In short the question is how do the retain color when text is redirected to a file.

ls -l --color|less -R would retain color

but how to I retain color when

someunixcommandwhich prints colored text>filename
# 4  
Old 03-23-2014
You can add other conditions and use below
Code:
ls -l | awk '{if($1 ~ /^d/) {$9 = ("<FONT COLOR=\"GREEN\">" $9 "</FONT>")}
  else if($1 ~ /^l/) {$9 = ("<FONT COLOR=\"BLUE\">" $9 "</FONT>")}
  else if($1 ~ /x/) {$9 = ("<FONT COLOR=\"PURPLE\">" $9 "</FONT>")}
  else if($9 ~ /.gz$/ || $9 ~ /.Z$/) {$9 = ("<FONT COLOR=\"RED\">" $9 "</FONT>")}
  else {$9 = ("<FONT COLOR=\"WHITE\">" $9 "</FONT>")}; print}'

or use below and save the output to sample.html and open the html file to see colors

Code:
ls -l | awk '{if($1 ~ /^d/) {$9 = ("<FONT COLOR=\"BLUE\">" $9 "</FONT>")}
  else if($1 ~ /^l/) {$9 = ("<FONT COLOR=\"PURPLE\">" $9 "</FONT>")}
  else if($1 ~ /x/) {$9 = ("<FONT COLOR=\"GREEN\">" $9 "</FONT>")}
  else if($9 ~ /.gz$/ || $9 ~ /.Z$/) {$9 = ("<FONT COLOR=\"RED\">" $9 "</FONT>")}
  else {$9 = ("<FONT COLOR=\"BLACK\">" $9 "</FONT>")}; print $0 "<BR>"}' > sample.html


Last edited by SriniShoo; 03-23-2014 at 10:48 AM.. Reason: changed color from white to black and added "<BR>" tag for html
# 5  
Old 03-23-2014
I can recommend pixelbeat's ansi2html.sh

Code:
ls -rlt --color | ansi2html.sh > king.html

# 6  
Old 03-24-2014
Thanks mate for your reply,however the shell script I am executing does not have --color option,its a custom script written which is printing colors using awk and echo

Since ls has --color options that works fine but what if a custom script does not have color option,Thanks

Code:
ls -rlt --color | ansi2html.sh > king.html

# 7  
Old 03-24-2014
Well using ls was just an example, the reason --color is needed for the ls command is that it normally detects that the output isn't a terminal and avoids outputting any color escape sequences. This is probably to simplify scripts that process the ls output.

Now unless your custom script has a similar feature i.e checking stdout and only outputting plain text when it's not a terminal this should all be fine.

Checking this script ansi2html.sh script it does require python, however it should be a pretty trivial exercise to change it to use awk instead.

Edit: Re-reading your posts I'm guessing your script does check the output type and only print the color escape sequences if the output is a terminal. It's probably checking [-t 1] or something similar. I've encountered this issue before myself and found using ssh is a nice workaround.

Say your script is called output_color then you could call it like this:

Code:
$ ssh -t localhost /usr/local/bin/output_color | ansi2html.sh > king.html

or using ls and an example:

Code:
$ ssh -t localhost "cd $PWD; ls" | ansi2html.sh > king.html


Last edited by Chubler_XL; 03-24-2014 at 11:12 AM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send mail with font color change

Hi All, I have a file that contains following entries. I want to highlight the line that has word as "FAILURE" while sending the email. File ------------------------------------------------------------ Job Name: ABC Start Time: 07/20/2019 07:32:39 End Time: 07/20/2019... (4 Replies)
Discussion started by: sdosanjh
4 Replies

2. UNIX for Dummies Questions & Answers

How to change the background color in the init 3 mode(not line color)

Hello, I am using RHEL 6.1 on VMware I am searching for a way to change background color (not line by line color wich one can using tput command) basically changing the color of the whole screen to white instead of the default black and changing font color to black and alos would like to... (2 Replies)
Discussion started by: Dexobox
2 Replies
Login or Register to Ask a Question