您应该将变量放在引号周围以防止分词。它导致单个参数变为两个或更多:
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