1

We need to write a BASH script to perform a SQL query and then send an automated e-mail using some of the results. Our SQL version is Oracle8i Enterprise Edition Release 8.1.7.4.0 and the SELECT statement to get the results that we want is:

SELECT kred_lim.kunr, kust_adr.ku_email, kred_lim.kred_limit, kred_lim.kred_zu_zahlen
FROM kred_lim, kust_adr
WHERE kred_lim.kred_zu_zahlen > kred_lim.kred_limit
AND kred_lim.kunr = kust_adr.ku_nr;

So this will show the customer number, customer e-mail address, credit limit and current outstanding orders value limited to customers who's outstanding orders value is greater than their credit limit. We are just stuck on where to start, or how to incorporate those results into an e-mail.

We just need a simple e-mail sent to each one, explaining that they're over their credit limit and showing their credit limit and current outstanding orders value in that e-mail.

Any examples or pointers would be appreciated.

4

1 回答 1

2

我会写:

#!/bin/bash

sql="
    set pagesize 0
    set feedback 0

    SELECT kred_lim.kunr ||','|| kust_adr.ku_email ||','|| kred_lim.kred_limit ||','|| kred_lim.kred_zu_zahlen
    FROM kred_lim, kust_adr
    WHERE kred_lim.kred_zu_zahlen > kred_lim.kred_limit
    AND kred_lim.kunr = kust_adr.ku_nr;
"

email_template="Subject: Credit limit
To: %s

Customer number: %s
Credit limit: %s
Current orders: %s
"

sqlplus $user/$pass@$db <<< "$sql" |
while IFS=, read -r num email limit orders; do
    printf "$email_template" "$email" "$num" "$limit" "$orders" |
    /path/to/sendmail -f sender@example.com -oi -t
done
于 2013-07-03T20:31:22.730 回答