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 :confused:.

Regards
Kingcobra

---------- Post updated 03-23-14 at 12:11 AM ---------- Previous update was 03-22-14 at 09:37 PM ----------

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

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...

-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
1 Like

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

You can add other conditions and use below

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

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

I can recommend pixelbeat's ansi2html.sh

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

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

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

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:

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

or using ls and an example:

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

Lovely,you are a rockstar mate,Big thanks for yor help
cheers

---------- Post updated 03-26-14 at 01:00 PM ---------- Previous update was 03-25-14 at 02:00 PM ----------

@"Chubler XL" The command you have given is working 100% percent perfectly mate,I was looking at ansi2html script,was trying to convert this script only to work using shell and awk ,as I am not permitted to install new packages in my office server.I tried pretty hard yesterday but it dint work well.Please if possible help me on this(I am from java background,my unix knowledge is of begginer range)
Thanks for the help you have done so far,really appreciate the time you are spending to help novices and begginers in this forum!!, you rock.

The attached updated version should work. It does use multi-byte characters and some awk implementations may not like this, I've only tested with GNU awk.

1 Like

I've replaced to attached file with a new version that avoids using UTF8 characters in awk so it should be more compatible with older awk implementations. However it looks like GNU sed is also required by other parts of the script, so it is still unlikely to run on Solaris/AIX.

1 Like

You rock