bash 使用mailx发送附件的Bash脚本

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

Bash Script using mailx to mail attachment

bashshellcsvattachmentmailx

提问by DoCnTex

I have a bash script that runs a query in postgres, it outputs to csv. I want to add to that script to use mailx to email that .csv file to a particular email.

我有一个在 postgres 中运行查询的 bash 脚本,它输出到 csv。我想添加到该脚本以使用 mailx 将该 .csv 文件通过电子邮件发送到特定电子邮件。

The problem I am having is it will not email the file. I can get the email so I know mailx is setup correctly. I just cannot get it to email it as an attachment. It can also email the output in the body of the email.

我遇到的问题是它不会通过电子邮件发送文件。我可以收到电子邮件,所以我知道 mailx 设置正确。我只是无法将其作为附件通过电子邮件发送。它还可以通过电子邮件发送电子邮件正文中的输出。

So here is the code.

所以这是代码。

    #!/bin/bash
    NOWDATE=`date +%m-%d-%Y`
    PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o /tmp/folder/file-$NOWDATE.csv <<EOF
    Query is here

    # remove the first 2 lines of the report as they are headers
    sed -i '2d' /tmp/folder/file-$NOWDATE.csv

    uuencode /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

I have tried the mailx part with:

我已经尝试过 mailx 部分:

    uuencode /tmp/folder/file-$NOWDATE.csv /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

and

    uuencode /tmp/folder/file-$NOWDATE.csv file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

So the problem I get is it spits out this error when I run the .sh file.

所以我遇到的问题是当我运行 .sh 文件时它会吐出这个错误。

    uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error

回答by Raghuram

If the problem is with uuencode...why cant you try mailx -a option which can also attach the files to the mail. Check this linkfor more info.

如果问题出在 uuencode 上...为什么你不能尝试 mailx -a 选项,它也可以将文件附加到邮件中。查看此链接以获取更多信息。

回答by Keith Thompson

NOWDATE=`date +%m-%d-%Y`

It's up to you, but consider using ISO-8601 format, YYYY-MM-DD (%Y-%m-%d). Among other advantages, it sorts nicely.

这取决于您,但请考虑使用 ISO-8601 格式 YYYY-MM-DD ( %Y-%m-%d)。除其他优点外,它的分类很好。

# remove the first 2 lines of the report as they are headers
sed -i '2d' /tmp/folder/file-$NOWDATE.csv

This doesn't remove the first two lines, it just removes the second line. Change '2d'to '1,2d'(but see below).

这不会删除前两行,它只会删除第二行。更改'2d''1,2d'(但见下文)。

Note that this modifies the file in place.

请注意,这会就地修改文件。

uuencode /tmp/folder/file-$NOWDATE.csv | mailx [...]

If uuencodeis given only one file name, it reads from standard input and puts the name into its output. Your following text, "I have tried the mailx part with:" ..., indicates that you're probably aware of this -- but you haven't shown us the code that fixes that issue other than in snippets.

如果uuencode只给出一个文件名,它会从标准输入中读取并将名称放入其输出中。您的以下文本,“我已经尝试过使用 mailx 部分:”...,表明您可能已经意识到这一点——但除了片段之外,您还没有向我们展示修复该问题的代码。

The error message you're getting:

您收到的错误消息:

uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error

isn'twhat you'd normally get if the file doesn't exist. I don't know what would cause an "Unknown system error" like that.

没有什么,如果该文件不存在,你通常得到的。我不知道什么会导致这样的“未知系统错误”。

But here's an alternative that (a) is a bit cleaner IMHO, and (b) doesn't require uuencodeto attempt to read the file:

但这里有一个替代方案,(a) 恕我直言更干净,(b) 不需要uuencode尝试读取文件:

#!/bin/bash

NOWDATE=`date +%m-%d-%Y` # but %Y-%d-%m is better
DIR=/tmp/folder
FILE=file-$NOWDATE.csv
[email protected]

PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o $DIR/$FILE <<EOF
... Query is here
EOF

tail -n +3 $DIR/$FILE | uuencode $FILE | \
    mailx -s "Accounts No Credit Card Report for '$NOWDATE'" $RECIPIENT

回答by Ketan.G

I had the same issue. A bash script executing the query, saving the csv file and mailing it. In my case it it gave the uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error

我遇到过同样的问题。执行查询、保存 csv 文件并邮寄的 bash 脚本。就我而言,它给了uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error

When I executed the script using the ksh shell, it worked perfectly fine without any issues. Like this - ksh script.shThis is just another pointer. In case uuencode gives error, then try executing it using ksh; it might work for you.

当我使用 ksh shell 执行脚本时,它运行良好,没有任何问题。像这样 -ksh script.sh这只是另一个指针。如果 uuencode 出错,则尝试使用 ksh 执行它;它可能对你有用。