bash 使用mailx在电子邮件中以附件形式发送多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26755628/
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 11:42:58  来源:igfitidea点击:

sending multiple files as attachment in e-mail using mailx

bashshellunixmailx

提问by DivB

I have a requiremnet to send multiple files as e-mail attachmnet in shell script. I have used below command.

我有一个要求在 shell 脚本中将多个文件作为电子邮件附件发送。我使用了以下命令。

(printf "%s\n" "BODY"; uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt ) | mailx -m -s "TEST" [email protected]

However the number of files i want to send as an attachmnet are dynamic. So I want to assign the uuencode ... comand to a variable and then use it. I have tried below way,

但是,我想作为附件发送的文件数量是动态的。所以我想将 uuencode ... 命令分配给一个变量,然后使用它。我试过以下方式,

$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt

$ (printf "%s\n" "BODY"; $ATTACH_CMD ) | mailx -m -s "TEST" [email protected]

And i am getting below error.

我得到低于错误。

sh: uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt:  not found.

Can any one please help me with this? Thanks in advance.

任何人都可以帮我解决这个问题吗?提前致谢。

采纳答案by DivB

I finally found the way. eval makes the trick

我终于找到了方法。eval 成功

eval $STR 

$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt

$ (printf "%s\n" "BODY"; eval $ATTACH_CMD ) | mailx -m -s "TEST" [email protected]

回答by KG3

Have you tried using the below code? Not sure why it works, but maybe the below code could be used as a workaround

您是否尝试过使用以下代码?不知道为什么它有效,但也许下面的代码可以用作解决方法

(printf "%s\n" "BODY"; `echo $ATTACH_CMD` ) | mailx -m -s "TEST" [email protected]`?

For $ATTACH_CMDI have used echocommand.

因为$ATTACH_CMD我用过echo命令。