$(tail) 到变量删除 \n

who*_*ami 1 bash email tail

我正在做以下事情:

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

Perl 脚本:

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

当我收到电子邮件时,日志文件\n<pre>标签内没有任何内容。在Hello to the world of tomorrow正常工作和\n表示。

我如何才能tail不删除任何\n,或者它是链条下游/上游的东西?

Nah*_*eul 5

命令 echo 中缺少双引号:

echo -e "$x" | ./perlscript 
Run Code Online (Sandbox Code Playgroud)

  • @whoami,在 shell 手册页中查找“分词”——当你不引用变量时,所有的空格序列都被一个空格替换。 (2认同)

jw0*_*013 5

这是一种更简洁、更惯用的方式来完成您正在尝试做的事情:

{ printf 'Hello to the world of tomorrow\n <pre>\n'
  tail -n 50 log/logfile.log
  printf '</pre>\n'; } | ./perlscript
Run Code Online (Sandbox Code Playgroud)

无论如何,当您只是要按顺序将其推送到管道中时,没有理由首先将所有输出缓冲在变量中。