I am sending multiple files in a single mail to myself:
file1="/somedir/file1.md"
file2="/somedir/file2.md"
file3="/somedir/file3.md"
file4="/somedir/file4.md"
echo "Body text" | mailx -s "The Files" -a "${file1}" -a "${file2}" -a "${file3}" -a "${file4}" recipient@domain.com
These files are not always there, so if any of these attachments does not exist the mail will not be send. However I want to send the mail anyway with the attachments that are there. I could build an if statement with multiple other if statements nested inside of it, but that seems a bit devious. What is a better way?
You can build -a
options dynamically by filtering out non-existent files:
files_arg=
for file in /somedir/file1.md /somedir/file2.md /somedir/file3.md /somedir/file4.md ~/.bashrc; do
[[ -f $file ]] && files_arg="$files_arg -a $file"
done
echo $files_arg
Outputs:
-a /home/max/.bashrc