bash 替换附件的 ${} 占位符

bash to replace ${} placeholder for attachment

我一直在尝试使用 MIME-multipart 编码发送纯文本和使用 postfixHTML 版本的消息。

我在 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}//" \
    -e "s/${attachment}/$ATTACHMENT/" \
    -e "s/${filename}/$FILENAME/" temp-email > $EMAIL

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

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

提前致谢。

当你运行文件上的命令足够长时

$ 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命令的末尾不在同一行

如何解决:

只需在使用 tr -d'\n' 之类的命令将 \n 传递给 sed 之前将其删除,这对 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>

测试:

$ 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--

大致上,您可以使用需要 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