0

I wish to use an echo command into my bash script.

Actually I do that way =>

echo --- REPORT ---
echo - Report send @ $(date +%d-%m-%y_%H:%M)
echo "echo \"Subject: BACKUP-$serv\"" | cat $cheminlogs/backup-$serv.log | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;   
echo - Report done @ $(date +%d-%m-%y_%H:%M)

But that's don't work when I call my sh file, that's print the echo linke but won't send me the email into the 3rd line command

If I use my command alone that's ok

echo "Subject: BACKUP-$serv" | cat $cheminlogs/backup-$serv.log | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;

I don't know how to use the echo in the start of a new command

Perhaps I can change my email command by invertion argument but that's send me an email without textarea cat

cat $cheminlogs/backup-$serv.log | echo "Subject: BACKUP-$serv" | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;
codeforester
  • 39,467
  • 16
  • 112
  • 140
WolwX
  • 121
  • 1
  • 15
  • The answer bellow with printf won't work for me, and finally I found how to escape echo, by this way that's work correctly => echo 'Subject:' "BACKUP-$serv" | cat - "$cheminlogs/backup-$serv.log" | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2 – WolwX Aug 18 '18 at 09:10

1 Answers1

2

The form you are looking for is

printf 'Subject: %s\n\n' "$subject" | cat - "$server_log" | sendmail

The - means 'read from standard input'. Note that sendmail will expect an empty line between the header and the message body.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Jon
  • 3,573
  • 2
  • 17
  • 24
  • `echo -e` is not at all portable -- much better to use, say, `printf 'Subject: %s\n\n' "$subject"`. See the APPLICATION USAGE section of the POSIX `echo` spec at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html. Just as importantly, this means you can send messages with backslashes inside the subject without having `echo` mangle them. – Charles Duffy Aug 16 '18 at 13:59
  • @CharlesDuffy Thanks, corrected! – Jon Aug 16 '18 at 14:10