Pipe the output from three echo statement to mail Pipe the output from three echo statement to mail shell shell

Pipe the output from three echo statement to mail


Your requirement is not completely clear, but try this

{    echo "Total items: `echo $QUERY1 | awk '{print $1}'`"    echo "Total Error: `echo $QUERY1 | awk '{print $2}'`"    echo "Percentage: $QUERY2"} | mail -s "subject" toUser1@xyz.com,toUser2@abc.com

The { .. } pair creates a process group, and all std-output is redirected into the 1 | (pipe), which connects to the std-in of your mail program.

You may need to use mailx, -s specifies subject, which I see from your other question on this topic that you seem to understand.

Also sendmail will need to be running and properly configured for any mail to be delivered from the machine that you execute this script.

IHTH


Edit: 2015-11-07

Just got a 'nice answer' star for this, and on on review, I'm surprised that I didn't comment on excessive use of processes. For this case, this can be reduced to one call to awk, i.e.

awk -v q1="$QUERY1" -v q2="$QUERY2" \ 'END {    split(q1,q1arr)    print "Total items: " q1arr[1] \          "Total Error: " q1arr[2] \          "Percentage: " q2}' /dev/null \| mail -s "subject" toUser1@xyz.com,toUser2@abc.com

Or for the one-liner crowd ;-), that is

awk -v q1="$QUERY1" -v q2="$QUERY2" 'END {split(q1,q1arr);print "Total items: " q1arr[1] "\nTotal Error: " q1arr[2] "\nPercentage: " q2 }' /dev/null | mail -s "subject" toUser1@xyz.com,toUser2@abc.com

The { .. } aren't needed in this case, as there is only one process connecting to the pipe.

For a case like a summary report being sent once a day, the original code is completely usable (but non-optimal). However, coding non-optimally leads to bad habits. Calling 5 processes when one will suffice in a loop that runs 1000s of times in a day, will consume compute resources unnecessarily.

Finally, as the o.p. didn't include any sample data, the code is only lightly tested.


Just create a function in bash and | (pipe) it to sendmail.

            #!/bin/bash            echo_statement(){            echo "Total items: `echo $QUERY1 | awk '{print $1}'`"            echo "Total Error: `echo $QUERY1 | awk '{print $2}'`"            echo "Percentage: $QUERY2"            }            echo_statement | mail -s "subject" you@yourdomain.com