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
  • 71,734
  • 34
  • 193
  • 226
whoami
  • 3,750
  • 7
  • 27
  • 26
  • 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 
Nahuel Fouilleul
  • 1,554
  • 10
  • 8
  • Thats the one....Would be nice to understand why the differnce between `echo -e "$x"` and `echo -e $x` – whoami Sep 26 '12 at 15:12
  • 2
    @whoami, look in the shell man page for "word splitting" -- when you don't quote the variable, all sequences of whitespace are replaced by a single space. – glenn jackman Sep 26 '12 at 15:35
  • I don't think this is the only problem. I think that `$(...)` constructs also drop the trailing space. Witness that `A=$(printf "abc\n"); printf "<%s>" "$A"` will produce ``. – dubiousjim Sep 26 '12 at 16:03
  • I agree but this error had to be identified – Nahuel Fouilleul Sep 26 '12 at 16:35
  • @whoami Always use double quotes around variable substitutions and command substitutions: `"$x"`, `"$(somecommand)"`. See http://mywiki.wooledge.org/BashFAQ/020,[$VAR vs ${VAR} and to quote or not to quote](http://unix.stackexchange.com/q/4899) – Gilles 'SO- stop being evil' Sep 26 '12 at 23:03
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
  • 50,274
  • 9
  • 137
  • 141