Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
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

2 Answers 2

up vote 3 down vote accepted

double quotes are missing in the command echo :

echo -e "$x" | ./perlscript 
share|improve this answer
    
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 <abc>. –  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 mywiki.wooledge.org/BashFAQ/020,$VAR vs ${VAR} and to quote or not to quote –  Gilles Sep 26 '12 at 23:03

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.

share|improve this answer
    
Oh that is a much cleaner method :D Thanks –  whoami Sep 26 '12 at 15:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.