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"
Author by
Srinivasarao Kotipatruni
Updated on September 18, 2022Comments
-
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 almost 5 yearsBTW, why even run the
find
when you already have the list of filenames in$FILE_ARR
? -
Srinivasarao Kotipatruni almost 5 yearsBecause 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 almost 5 yearsI 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 almost 5 yearseven something as simple as
ls -1d "${FILE_ARR[@]}" 2>/dev/null
would be better than using find here. -
Srinivasarao Kotipatruni almost 5 yearsI 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 almost 5 yearsthat's what the
2>/dev/null
is for - it redirects all error messages (stderr, i.e. file descriptor 2) fromls
to /dev/null. -
Srinivasarao Kotipatruni almost 5 yearsThank 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 almost 5 yearsls is printing directories as well. So, tried this: ls -1dp ${FILE_ARR[@]} 2>/dev/null | grep -v /
-
-
Srinivasarao Kotipatruni almost 5 yearsThank You so much for the answer. This works for me.
-
ilkkachu about 2 years
$(echo ${FILE_ARR[@]} | sed...)
will blow up badly if the filenames contain whitespace or glob characters. -
ilkkachu about 2 years
$(echo ${FILE_ARR[@]} | sed...)
will blow up badly if the filenames contain whitespace or glob characters. -
Alessio about 2 yearsIn 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 about 2 yearsEdited 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 about 2 years@cas, I think it does work for multiple filenames (without space or glob chars). The
echo
will print them asfoo bar
, the sed turns that intofoo -o -name bar
and word splitting makes that work right. And well, seemed to work on a quick test anyway. -
ilkkachu about 2 yearserr, yeah, just that the sed was necessary for it to work even with nice filenames. Now if the array has
foo
andbar
, it turns intofind ... -name foo bar
, which just doesn't work with find. -
Luis Talora about 2 yearsYep... 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!