Wildcard in mailx argument

Hi All,

I have to send some files as attachments to an email using mailx copmmand in a shell script.

The files will be generated by some other application everyday with names starting with the literal 'Send' followed by some random sequence of characters in the filenames.

I tried using * wildcard like below:

mailx -a /dir/Send* user@domain.com

However, while executing the script, it picks up only the first file it finds matching the filename and does not pickup remaining files with the similar name format.

The number of files will be varying every day as like their filenames. Only string common in the filenames will be 'Send' in the beginning. I have to send all the files in the directory matching the filenames Send*

Can anyone please help me out? Thanks in advance.

I assume the -a option means: MIME-attach the given file.
You have to also put a body text like this

echo "Hi!
please find the attached files" | mailx -a /dir/Send1 user@domain.com

For multiple attached files you must put a -a before each file name

echo "Hi!
please find the attached files" | mailx -a /dir/Send1 -a /dir/Send2 user@domain.com

I don't recall a bash builtin that could do this for all present files, but a printf sub command can do it

echo "Hi!
please find the attached files" | mailx `printf " -a %s" /dir/Send*` user@domain.com

This will fail if there is no attachment file (or non-files e.g. a directory).
A fail-safe implementation is a loop that puts up a string that is passed to mailx

attach=""
for fn in /dir/Send*
do
  [ -f "$fn" ] && attach="$attach -a $fn"
done
echo "Hi!
please find the attached files" | mailx $attach user@domain.com
1 Like