简体   繁体   中英

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.

-- 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. 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:

No such file or directory

I can run the same mailx command from console and have the e-mail sent out. 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. I typed parts of the script today again while troubleshooting and that fixed the issue. But the script above is good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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