1

早上,我想通过 mutt 发送带有来自文本文件的附件列表的电子邮件。

这是我的代码:

#!/bin/bash
subj=$(cat /home/lazuardi/00000000042/subject.txt)
attc=$(find /home/lazuardi/00000000042 -name "*.*" | grep -v body.txt | grep -v email.txt | grep -v subject.txt | grep -v body.html > attachment.txt)
ls=$(for x in $attc; do read; done)
while read recp; do
    while read ls; do
        mutt -e "set content_type=text/html" $recp -s "$subj" -- $ls < /home/lazuardi/00000000042
    done < /home/lazuardi/attachment.txt
done < /home/lazuardi/00000000042/email.txt 

我仍然无法在 attachment.txt 中附加文件,我尝试使用 FOR LOOP,结果相同。我应该怎么做?

4

1 回答 1

1

您应该将变量放在引号周围以防止分词。它导致单个参数变为两个或更多:

mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$ls" < /home/lazuardi/00000000042

而且我不确定从目录中读取输入?

/home/lazuardi/00000000042

这里的作业也没有意义:

attc=$(find /home/lazuardi/00000000042 -name "*.*" | grep -v body.txt | grep -v email.txt | grep -v subject.txt | grep -v body.html > attachment.txt)
ls=$(for x in $attc; do read; done)

试试这个:

#!/bin/bash

subj=$(</home/lazuardi/00000000042/subject.txt)

attachments=()
while IFS= read -r file; do
    attachments+=("$file")
done < <(exec find /home/lazuardi/00000000042 -name "*.*" | grep -v -e body.txt -e email.txt -e subject.txt -e body.html)

echo '---- Attachments ----'
printf '%s\n' "${attachments[@]}"
echo

recipients=()
while read recp; do
    recipients+=("$recp")
done < /home/lazuardi/00000000042/email.txt

echo '---- Recipients ----'
printf '%s\n' "${recipients[@]}"
echo

for recp in "${recipients[@]}"; do
    for attachment in "${attachments[@]}"; do
        echo "Sending content to $recp with subject $subj and attachment $attachment."
        mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$attachment" < /home/lazuardi/00000000042/body.txt
    done
done

如果 Bash 版本是 4.0+,可以简化为:

#!/bin/bash

subj=$(</home/lazuardi/00000000042/subject.txt)

readarray -t attachments \
    < <(exec find /home/lazuardi/00000000042 -name "*.*" | grep -v -e body.txt -e email.txt -e subject.txt -e body.html)

echo '---- Attachments ----'
printf '%s\n' "${attachments[@]}"
echo

readarray -t recipients < /home/lazuardi/00000000042/email.txt

echo '---- Recipients ----'
printf '%s\n' "${recipients[@]}"
echo

for recp in "${recipients[@]}"; do
    for attachment in "${attachments[@]}"; do
        echo "Sending content to $recp with subject $subj and attachment $attachment."
        mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$attachment" < /home/lazuardi/00000000042/body.txt
    done
done
于 2014-08-15T04:28:03.547 回答