如何在 bash 脚本中为 sendmail 保留换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21560472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:27:56  来源:igfitidea点击:

How do I preserve new line characters for sendmail in bash script?

bashechosendmail

提问by Charlie

Why isn't the following preserving the new line characters in the resulted email?

为什么以下不保留结果电子邮件中的换行符?

#!/bin/bash

file="/tmp/ip.txt"
address=$(curl -s http://ipecho.net/plain; echo)
ifconfig=$(ifconfig)

function build_body
{
    echo "----------------------------------------------------------------" > $file
    echo "IP Address: $address (according to http://ipecho.net/plain)" >> $file
    echo "----------------------------------------------------------------" >> $file
    echo >> $file
    echo "Result from ifconfig:" >> $file
    echo >> $file
    echo "$ifconfig" >> $file
    echo >> $file
}

build_body
msg=$(cat $file)
mail="subject:Home Server Status\nfrom:[email protected]\n$msg"
echo $mail | /usr/sbin/sendmail "[email protected]"

I receive the email this script generates, however, the whole body is all on one line! /tmp/ip.txt is exactly how I want the email to look.

我收到了此脚本生成的电子邮件,但是,整个正文都在一行上!/tmp/ip.txt 正是我希望电子邮件的外观。

回答by postsasaguest

I ran into this recently. I was able to resolve it adding the '-e' option to echo.

我最近遇到了这个。我能够通过添加'-e'选项来解决它。

Change this:

改变这个:

echo $mail | /usr/sbin/sendmail "[email protected]"

回声 $mail | /usr/sbin/sendmail "[email protected]"

To This:

对此:

echo -e "$mail" | /usr/sbin/sendmail "[email protected]"

echo -e "$mail" | /usr/sbin/sendmail "[email protected]"

Hopefully that helps.

希望这有帮助。

回答by AnFi

You may use "here documents" (<<END), make the function output its results to standard output.

您可以使用“此处文档”( <<END),使函数将其结果输出到标准输出。

#!/bin/bash

address=$(curl -s http://ipecho.net/plain; echo)

function build_body
{
cat <<END
----------------------------------------------------------------
IP Address: $address (according to http://ipecho.net/plain) 
----------------------------------------------------------------

Result from ifconfig:

END
ifconfig
echo    
}


( cat <<END; build_body) | /usr/sbin/sendmail -i -- "[email protected]"
Subject:Home Server Status
From:[email protected]

END

回答by grebneke

  1. Use double-quotes: echo "$mail" | /usr/bin/sendmail ...
  2. Shouldn't that be two \nbetween the headers and message:
  1. 使用双引号: echo "$mail" | /usr/bin/sendmail ...
  2. \n标题和消息之间不应该是两个:

as in:

如:

mail="subject:Home Server Status\nfrom:[email protected]\n\n$msg"

回答by John1024

{
    printf "subject:Home Server Status\nfrom:[email protected]\n\n"
    cat "$file"
} | /usr/sbin/sendmail "[email protected]"

With echo $mail, the contents of the file appear on the command line and bash processes them with word expansion. With cat "$file", the file name appears on the command line but the contents of the file do not and are thus safe from bash.

使用echo $mail,文件的内容出现在命令行上,bash 用单词扩展处理它们。使用cat "$file",文件名出现在命令行上,但文件的内容不会出现,因此不受 bash 的影响。