I'm writing a simple Bash script to execute a backup and send a mail at the end with a modified subject if the command has been failed.
This is a snippet of the script:
BACKUP_CMD="ls -1" # Just an example command
MAIL_RECIPIENTS="me@domain" # Recipient addresses (space separated list)
MAIL_SUBJECT="Backup script" # Mail subject
MAIL_FAIL_SUBJECT_PREPEND="[!!! ---FAILURE--- !!!] "
MAIL_CMD="mail -a \"Content-Type: text/plain; charset=UTF-8\" -s \"SUBJECT\" RECIPIENTS" # Mail send command
# Execute backup
output="$($BACKUP_CMD)"
status=$?
# Check exit status code and compose the mail message
echo $status
if [ "$status" -eq "0" ]; then
mail_subject=$MAIL_SUBJECT
mail_text="${output}"
else
mail_subject="$MAIL_FAIL_SUBJECT_PREPEND $MAIL_SUBJECT"
mail_text="$output\n\nExit status code: $status"
fi
# Compose the mail command with subject and recipients list
mail_cmd=$MAIL_CMD
mail_cmd="${mail_cmd/SUBJECT/$mail_subject}"
mail_cmd="${mail_cmd/RECIPIENTS/$MAIL_RECIPIENTS}"
echo "$mail_text" | ${mail_cmd}
The mail is not sent and the script exits with this error from send-mail:
send-mail: RCPT TO:<Backup", charset=UTF-8"@server.domain> (501 5.1.3 Bad recipient address syntax)
Can't send mail: sendmail process failed with error code 1
It seems that bash "scrambles" arguments at the end of the mail command.
What I cannot understand is that if I print $mail_cmd it seems to be correct:
mail -a "Content-Type: text/plain; charset=UTF-8" -s "Backup script" me@domain
Where I'm wrong?
BACKUP_CMD="ls -1"andMAIL_CMD="mail -a .... Use bash arrays.MAIL_CMD=(mail -a "blabla" "blablabla").