How to send the result of a query by e-mail SHELL SCRIPT

Asked

Viewed 412 times

1


I have the following appointment:

#!/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 take this result and send by email, ?

I tried that too:

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

Plus gave the following mistake:

./query.sh: line 14: $campanha: ambiguous redirect

2 answers

2


The sendmail is a good alternative, but I usually use the mail same, in BASH, for already coming in a series of distros and be ready to use.

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

File example:

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, there are flags for attachments, and other interesting things, see the manuals:

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.