0
0
0
s2sdefault

Previously, I wrote a script that ran the fortune and cowsay commands and sent the output as an email. I have this script in a cron job, so I get a comical "fortune" sent to my email once a day. Well, after a few days, I noticed that one of the cowsay characters wasn't coming through properly. It turns that one of the characters had an HTML comment tag within it. Here's how I found it, and solved it.

It actually took a few weeks for me to really notice the issue. For starters, the cowsay character is chosen at random, so you never know what you'll get next. The first time I got the problem email, I just disregarded it as something weird. The second time though, caught my eye, but I took another look through the script, and rechecked my logic, and everything seemed just fine. The third time, I knew something was up. For the sake illustration, here's the kind of email I was getting:

______________________________________
/ You have an unusual understanding of \
\ the problems of human relationships. /
 --------------------------------------
    \
     \
                                   .::!!!!!!!:.
  .!!!!!:.                        .:!!!!!!!!!!!!
  ~~~~!!!!!!.                 .:!!!!!!!!!UWWW$$$
      :$$NWX!!:           .:!!!!!!XUWW$$$$$$$$$P
      $$$$$##WX!:      . 

The first time I saw this I just couldn't make out what I was supposed to be seeing, so it was disregarded. The third time though, I figured something wasn't quite right. My work mandates the use of Microsoft Outlook (ewwww, I know), and fortunately for me, you can right-click the HTML formatted email, and "view source". Here's what I saw:

<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head>
<body><pre style="font-family:&quot;Courier" New?=""> ______________________________________
/ You have an unusual understanding of \
\ the problems of human relationships. /
--------------------------------------
    \
     \
                                   .::!!!!!!!:.
  .!!!!!:.                        .:!!!!!!!!!!!!
  ~~~~!!!!!!.                 .:!!!!!!!!!UWWW$$$
      :$$NWX!!:           .:!!!!!!XUWW$$$$$$$$$P
      $$$$$##WX!:      .<!!!!UW$$$$"  $$$$$$$$#
      $$$$$  $$$UX   :!!UW$$$$$$$$$   4$$$$$*
      ^$$$B  $$$$\     $$$$$$$$$$$$   d$$R"
        "*$bd$$$$      '*$$$$$$$$$$$o+#"
             """"          """"""" </pre></body></html>

Hey waitaminute... The character is complete here! What's going on? Well, it turns out that '<!' is an HTML Comment tag. This would explain why I could see everything right up to those characters. Ok great... So now I know the problem, on to devise a solution.

All I need to do is change the '<' to '&lt;'. Simple enough... The 'sed' command should be able to handle this reasonably easily enough... Something like:

sed "s/</\&lt;/g"

While I suppose that this command would be perfect, if used in the proper manner. But given that the '<' character will exst a lot due to the actual HTML tags, I'm going make the sed command a little more specific, and swap '<!' for "&lt;!':

sed "s/<\!/\&lt;\!/g"

It took me a few times to work it out, but eventually managed to get it right with the following command (and the variants I tested with):

echo '.<!!' | sed "s/<\!/\&lt;\!/g"

Now, all I needed to do was add the proper syntax to the script:

FORTUNE=$(echo "$FORTUNE" | sed "s/<\!/\&lt;\!/g")

Et voila, fini!

Now all I have to do is wait for the next iteration of the 'eyes' character (and all the others for that matter) to come through.

Feel free to download the script below, and provide me feedback in the comments.

Downloads:
zip-16 email_fortune_v3 3 HOT
Date-16Friday, 01 April 2016 15:40 File Size-16 995 B Download-16 383 Download

Add comment


Security code
Refresh

0
0
0
s2sdefault