Search code examples
bashshellunixmailx

sending multiple files as attachment in e-mail using mailx


I have a requiremnet to send multiple files as e-mail attachmnet in shell script. I have used below command.

(printf "%s\n" "BODY"; uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt ) | mailx -m -s "TEST" [email protected]

However the number of files i want to send as an attachmnet are dynamic. So I want to assign the uuencode ... comand to a variable and then use it. I have tried below way,

$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt

$ (printf "%s\n" "BODY"; $ATTACH_CMD ) | mailx -m -s "TEST" [email protected]

And i am getting below error.

sh: uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt:  not found.

Can any one please help me with this? Thanks in advance.


Solution

  • I finally found the way. eval makes the trick

    eval $STR 
    
    $ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
    $ echo $ATTACH_CMD
    uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
    
    $ (printf "%s\n" "BODY"; eval $ATTACH_CMD ) | mailx -m -s "TEST" [email protected]