bash替换$ {}占位符以进行附件

时间:2018-06-05 02:33:23

标签: linux bash sed mime postfix-mta

我一直在尝试使用MIME-multipart编码使用HTML发送纯文本和postfix版本的邮件。

我在tmp目录中创建了一个temp-email.txt,其中包含以下内容:

From:Sender <xxxx@xxxx.com>
To:Recipient <xxxx@xxxx.com>
Subject: Result
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MixedBoundaryString"

--MixedBoundaryString
Content-Type: multipart/alternative; boundary="AlternativeBoundaryString"

--AlternativeBoundaryString
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Result

${detail}

--AlternativeBoundaryString
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
</head>
  <body>
    <h1>Result</h1>
      <p>${detail}</p>
  </body>
</html>
--AlternativeBoundaryString--
--MixedBoundaryString
Content-Type: text/plain
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="${filename}"

${attachment}

--MixedBoundaryString--

我有一个bash脚本替换${}中的temp-email.txt个占位符。

以下是我的bash脚本的一部分:

ATTACHMENT=$(base64 /tmp/result.txt)
EMAIL="/report/result.txt"

sed -e "s/\${result}/$1/" \
    -e "s/\${attachment}/$ATTACHMENT/" \
    -e "s/\${filename}/$FILENAME/" temp-email > $EMAIL

我的问题是我在更换$ {attachment}

时遇到以下错误
sed: -e expression #2, char 92: unterminated `s' command

提前致谢。

2 个答案:

答案 0 :(得分:2)

粗略地说,你可以使用需要gettext包的envsubst。注意:你在做什么类似于树枝。

示例:3个变量集

#!/bin/bash
#  sudo yum install gettext
detail="DETAIL"
filename="FILENAME"
attach=$(base64 /tmp/result.txt)
email="/report/result.txt"

export detail='somevalue' filename='someothervalue' attachment="$(echo $attach)"
MYVARS='$detail:$filename:$result:$attachment'

envsubst "$MYVARS" < temp-email > $email

答案 1 :(得分:1)

在文件上运行命令

$ base64 /tmp/result.txt 

它会产生这样的输出:

PEhpdD4KICAgPEhpdF9udW0+MTwvSGl0X251bT4KICAgPEhpdF9pZD5UUjpWNFUwTDVfOVJPU0k8
L0hpdF9pZD4KICAgPEhpdF9kZWY+Z25sfFY0VTBMNXwgVW5jaGFyYWN0ZXJpemVkIHByb3RlaW4g
W0hvbW8gc2FwaWVuXSBPWD0zOTMzMDUgR049Q0lDTEVfdjEwMDA4MTM2bWcgUEU9NCBTVj0xPC9I
aXRfZGVmPgogICA8SGl0X2FjY2Vzc2lvbj5UUjpWNFUwTDVfOVJPU0k8L0hpdF9hY2Nlc3Npb24+
CiAgIDxIaXRfbGVuPjM4ODwvSGl0X2xlbj4KPC9IaXQ+Cg==

此输出包含EOL个字符,打破您的命令:

 -e "s/\${attachment}/PEhpdD4KICAgPEhpdF9udW0+MTwvSGl0X251bT4KICAgPEhpdF9pZD5UUjpWNFUwTDVfOVJPU0k8
L0hpdF9pZD4KICAgPEhpdF9kZWY+Z25sfFY0VTBMNXwgVW5jaGFyYWN0ZXJpemVkIHByb3RlaW4g
W0hvbW8gc2FwaWVuXSBPWD0zOTMzMDUgR049Q0lDTEVfdjEwMDA4MTM2bWcgUEU9NCBTVj0xPC9I
aXRfZGVmPgogICA8SGl0X2FjY2Vzc2lvbj5UUjpWNFUwTDVfOVJPU0k8L0hpdF9hY2Nlc3Npb24+
CiAgIDxIaXRfbGVuPjM4ODwvSGl0X2xlbj4KPC9IaXQ+Cg==/"

并提供错误:

sed: -e expression #2, char 92: unterminated `s' command

因为s命令的结尾不在同一行。

如何解决:

只需删除\n,然后使用sed等命令将其传递给tr -d'\n',这对base64解码没有影响

示例:

$ cat /tmp/result.txt 
<Hit>
   <Hit_num>1</Hit_num>
   <Hit_id>TR:V4U0L5_9ROSI</Hit_id>
   <Hit_def>gnl|V4U0L5| Uncharacterized protein [Homo sapien] OX=393305 GN=CICLE_v10008136mg PE=4 SV=1</Hit_def>
   <Hit_accession>TR:V4U0L5_9ROSI</Hit_accession>
   <Hit_len>388</Hit_len>
</Hit>

使用

base64 /tmp/result.txt  | tr -d '\n'

如果再次解码:

$ base64 /tmp/result.txt | tr -d '\n' | base64 --decode
<Hit>
   <Hit_num>1</Hit_num>
   <Hit_id>TR:V4U0L5_9ROSI</Hit_id>
   <Hit_def>gnl|V4U0L5| Uncharacterized protein [Homo sapien] OX=393305 GN=CICLE_v10008136mg PE=4 SV=1</Hit_def>
   <Hit_accession>TR:V4U0L5_9ROSI</Hit_accession>
   <Hit_len>388</Hit_len>
</Hit>

<强> TEST:

$ sed "s/\${attachment}/$(base64 /tmpt/result.txt| tr -d '\n')/" email_template 
From:Sender <xxxx@xxxx.com>
To:Recipient <xxxx@xxxx.com>
Subject: Result
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MixedBoundaryString"

--MixedBoundaryString
Content-Type: multipart/alternative; boundary="AlternativeBoundaryString"

--AlternativeBoundaryString
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Result

${detail}

--AlternativeBoundaryString
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
</head>
  <body>
    <h1>Result</h1>
      <p>${detail}</p>
  </body>
</html>
--AlternativeBoundaryString--
--MixedBoundaryString
Content-Type: text/plain
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="${filename}"

PEhpdD4KICAgPEhpdF9udW0+MTwvSGl0X251bT4KICAgPEhpdF9pZD5UUjpWNFUwTDVfOVJPU0k8L0hpdF9pZD4KICAgPEhpdF9kZWY+Z25sfFY0VTBMNXwgVW5jaGFyYWN0ZXJpemVkIHByb3RlaW4gW0hvbW8gc2FwaWVuXSBPWD0zOTMzMDUgR049Q0lDTEVfdjEwMDA4MTM2bWcgUEU9NCBTVj0xPC9IaXRfZGVmPgogICA8SGl0X2FjY2Vzc2lvbj5UUjpWNFUwTDVfOVJPU0k8L0hpdF9hY2Nlc3Npb24+CiAgIDxIaXRfbGVuPjM4ODwvSGl0X2xlbj4KPC9IaXQ+Cg==

--MixedBoundaryString--