How to echo multiple lines into email body while using mail command in linux?

7,023

You can group multiple commands into a sub-shell using parentheses ( and ), and then pipe the sub-shell's output into mail.

e.g. (with a few extra line feeds after the "Below..." text):

( printf '%s\n\n\n' "Below are list of files transfered"
  find . -maxdepth 1 -type f -name $(echo ${FILE_ARR[@]}| sed 's/ / -o -name /g')
) | mail -s "$some_subject" "$some_mail_id"

Or, if the find command isn't needed (and it doesn't seem like it is):

( printf '%s\n\n\n' "Below are list of files transfered"
  printf '%s\n' "${FILE_ARR[@]}"
) | mail -s "$some_subject" "$some_mail_id"
Share:
7,023
Srinivasarao Kotipatruni
Author by

Srinivasarao Kotipatruni

Updated on September 18, 2022

Comments

  • Srinivasarao Kotipatruni
    Srinivasarao Kotipatruni over 1 year

    I have to send some list of file names (result of find command) into email body along with a message saying 'Below are list of files transfered'. How can I combine this message along with above find result into mail body.

    Ex:

    echo "Below are list of files transfered" | mail -s "$some_subject" $some_mail_id
    
    find . -maxdepth 1 -type f -name $(echo ${FILE_ARR[@]}| sed 's/ / -o -name /g') | \
      mail -s "$some_subject" $some_mail_id
    

    I am able to individually do above commands, but cannot combine them into one.

    Please Help. Thank You.

    • Alessio
      Alessio almost 5 years
      BTW, why even run the find when you already have the list of filenames in $FILE_ARR ?
    • Srinivasarao Kotipatruni
      Srinivasarao Kotipatruni almost 5 years
      Because my $FILE_ARRcontain wildcards, not full file names. And not all entries in my $FILE_ARR guarantee the existance of files in local dir. Only files in local dir that match with wild cards/filenames in $FILE_ARR should be sent over mail.
    • Alessio
      Alessio almost 5 years
      I still suspect there's probably a better way of doing it - perhaps by redirecting the output of whatever you're using to transfer the files (cp, rsync, or whatever) to a log file, and then including that (or an extract of it transformed by sed or awk or something) in the email.
    • Alessio
      Alessio almost 5 years
      even something as simple as ls -1d "${FILE_ARR[@]}" 2>/dev/null would be better than using find here.
    • Srinivasarao Kotipatruni
      Srinivasarao Kotipatruni almost 5 years
      I can not use a log file here due to some reasons. At max I direct output to a variable. But, here also, using 'ls' is causing prompts saying 'No such file or directory' when there is no file in local dir matching entries in $FILE_ARR
    • Alessio
      Alessio almost 5 years
      that's what the 2>/dev/null is for - it redirects all error messages (stderr, i.e. file descriptor 2) from ls to /dev/null.
    • Srinivasarao Kotipatruni
      Srinivasarao Kotipatruni almost 5 years
      Thank You. Got your point. but, its working only when I delete double quotes around array as: ls -1d ${FILE_ARR[@]} 2>/dev/null
    • Srinivasarao Kotipatruni
      Srinivasarao Kotipatruni almost 5 years
      ls is printing directories as well. So, tried this: ls -1dp ${FILE_ARR[@]} 2>/dev/null | grep -v /
  • Srinivasarao Kotipatruni
    Srinivasarao Kotipatruni almost 5 years
    Thank You so much for the answer. This works for me.
  • ilkkachu
    ilkkachu about 2 years
    $(echo ${FILE_ARR[@]} | sed...) will blow up badly if the filenames contain whitespace or glob characters.
  • ilkkachu
    ilkkachu about 2 years
    $(echo ${FILE_ARR[@]} | sed...) will blow up badly if the filenames contain whitespace or glob characters.
  • Alessio
    Alessio about 2 years
    In fact, it will only work if FILE_ARR contains only one filename (with no whitespace or glob chars). Perhaps I should have just not bothered re-using the OP's original script...but the question and my answer were about how to group the output of multiple commands, not about correcting other mistakes in the script. The second version, without find, works as expected.
  • Luis Talora
    Luis Talora about 2 years
    Edited and shortened the "find" command line on the example below, after @ilkkachu's heads up on possible issues with files with spaces. Thanks, @ilkkachu!
  • ilkkachu
    ilkkachu about 2 years
    @cas, I think it does work for multiple filenames (without space or glob chars). The echo will print them as foo bar, the sed turns that into foo -o -name bar and word splitting makes that work right. And well, seemed to work on a quick test anyway.
  • ilkkachu
    ilkkachu about 2 years
    err, yeah, just that the sed was necessary for it to work even with nice filenames. Now if the array has foo and bar, it turns into find ... -name foo bar, which just doesn't work with find.
  • Luis Talora
    Luis Talora about 2 years
    Yep... Makes sense. Just removed the example and left it open for any sort of commands. The aim of my comment was more to suggest a way of sending multiline mail messages than anything else. Thanks!