简体   繁体   English

Shell脚本:循环调用mailx

[英]Shell Scripting: Calling mailx in a loop

I am trying to write a script using which I need to send multiple emails with a file as attachment per email. 我正在尝试编写一个脚本,使用该脚本需要发送多封电子邮件,每封电子邮件带有一个文件作为附件。 This is because of mail attachment size limitations. 这是由于邮件附件大小的限制。

I have zip files in a directory and they are file01.zip, file02.zip etc. and there will be about 4-5 of these files. 我在目录中有zip文件,它们是file01.zip,file02.zip等,这些文件大约有4-5个。

-- File count is normally passed in
numFiles=5
fileCounter=1
datestr="`date +"%m/%d/%Y"`"

while [ $fileCounter -le $numFiles ]
do
    SUBJECT_LINE="Weekly files ($fileCounter of $numFiles) - $datestr"

    echo "[`date`] E-mailing file ($fileCounter of $numFiles) ... "
    ZIPFILE="file0$fileCounter.zip"
    echo $ZIPFILE
    ls -ltr $ZIPFILE
    mailx -a "$ZIPFILE" \
          -r no-reply@host.com \
          -s "$SUBJECT_LINE" \
          $TO_LIST < /dev/null
    echo "[`date`] Done"
    fileCounter=$(( $fileCounter + 1 ))
done

I am trying to call mailx in a loop as you can see. 如您所见,我试图循环调用mailx。 I tried the following as well 我也尝试了以下

for file in file0*.zip
do
...


done

I am able to see the ZIPFILE names when I print them out using echo but the mailx command in the loop returns the following although the files are there: 使用echo将它们打印出来时,我可以看到ZIPFILE名称,但是尽管文件在那里,循环中的mailx命令仍返回以下内容:

No such file or directory

I can run the same mailx command from console and have the e-mail sent out. 我可以从控制台运行相同的mailx命令,并发送电子邮件。 I can also send one e-mail without a loop, but doing so inside a loop seems to cause an issue. 我也可以不循环发送一封电子邮件,但是在循环内进行发送似乎会引起问题。 Am I missing something? 我想念什么吗?

I likely had one or more characters not visible to the eye in the file name ($ZIPFILE) being passed in as attachment to mailx. 在作为mailx附件的文件名($ ZIPFILE)中,我可能看不到一个或多个字符。 I typed parts of the script today again while troubleshooting and that fixed the issue. 我今天在进行故障排除时再次输入了脚本的某些部分,从而解决了该问题。 But the script above is good. 但是上面的脚本很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM