1

I am doing the following:

x="Hello to the world of tomorrow\n <pre>";
x="${x}$(tail -n 50 log/logfile.log)"
x="${x}</pre>";
echo -e $x | ./perlscript

The Perl script:

#!perl
# perlscript
open(MAIL, "sendmail -t")
print MAIL "EMAIL HEADERS\n\n"
print MAIL <STDIN>
close(MAIL);

When I receive the email, the log file doesn't have any \n inside the <pre> tag. The Hello to the world of tomorrow works fine and the \n is represented.

How to I get tail not to remove any \n, or is it something further down/up the chain?

jasonwryan
  • 73,126
whoami
  • 3,870
  • Is the first code block a bashscript or something, or are you just typing it on the command line? – Bernhard Sep 26 '12 at 14:50
  • Do you actually want \n (2 characters) or a newline character? Keep in mind echo is very nonportable when it comes to handling backslashes. If you want predictable behavior use printf instead of echo. – jw013 Sep 26 '12 at 14:50
  • Yes i Want the \n, and its a bashscript, but could be command line – whoami Sep 26 '12 at 14:51
  • Sorry the echo should be echo -e – whoami Sep 26 '12 at 14:56
  • @whoami No, saying you want echo -e \n probably means you really want an actual newline, and echo -e \n is your attempt at getting one. – jw013 Sep 26 '12 at 15:19
  • Completely unrelated, but why are you embedding what looks to be HTML tags into a plain text email? That is not good form. – jw013 Sep 26 '12 at 15:32
  • 1
    @jw013 - Its not plain text, print MAIL "EMAIL HEADERS\n\n" was an abbreviation for HTML content type header, and all the to: from: subject: fields. Sorry was lazy to type :D – whoami Sep 26 '12 at 15:50
  • Oh ok, never mind then :) – jw013 Sep 26 '12 at 15:52

2 Answers2

5

double quotes are missing in the command echo :

echo -e "$x" | ./perlscript 
5

This is a much cleaner and more idiomatic way to go about what you are trying to do:

{ printf 'Hello to the world of tomorrow\n <pre>\n'
  tail -n 50 log/logfile.log
  printf '</pre>\n'; } | ./perlscript

There is no reason you need to buffer all that output in a variable first when you are just going to push it in order into the pipe anyways.

jw013
  • 51,212