How to send the result of an email query SHELL SCRIPT

1


I have the following query:

#!/bin/bash
db="bats";
table="promotion";
dbHost="192.168.0.246";
dbUser="root";
dbPass="root";
result='mysql -h $dbHost --user=$dbUser --password=$dbPass --skip-column-names -e "SELECT name FROM  $db.$table WHERE finish > now() ORDER BY promotion_key DESC"'

campanha=$(echo $result | tr " " "\n")


   echo "$campanhas"

I want to get this result and send it by email, how would I do this?

I tried this too:

sendmail -s "CAMPANHAS" [email protected] < $campanha

Plus gave the following error:

./query.sh: line 14: $campanha: ambiguous redirect
    
asked by anonymous 11.05.2016 / 23:53

2 answers

2

sendmail is a good alternative, but I usually use mail itself, in BASH, to already come in a series of distros and be ready to use.

Anyway, for basic submission, the syntax is similar, and both mail and sendmail can be used as the examples below.

Example with file:

cat conteudo.html | mail -s "Assunto" [email protected]

Example with variable:

echo $variavel | mail -s "Assunto" [email protected]

Applied to your case:

mysql -h ... etc "... BY promotion_key DESC" | mail -s "CAMPANHAS" [email protected]


If you need, have flags for attachments, and other interesting things, see the manuals:

26.05.2016 / 01:31
0
echo $campanha | sendmail -s "CAMPANHAS" [email protected] 
    
25.05.2016 / 11:03