linux shell實現從資料庫中查詢資料並把資料用csv格式發送郵件

來源:互聯網
上載者:User

$ORACLE_HOME/bin/sqlplus -S username/password@instanceName > /dev/null 2>&1 <<!!<br />#對sqlplus作一些設定,使之只輸出我們需要的文本<br />set echo off<br />set pagesize 0<br />set verify off<br />set feedback off<br />set termout off<br />set linesize 3000<br />set trimspool on<br />#查詢輸出到臨時檔案<br />spool /tmp/some_tmp.csv<br />#select語句<br />select column1||','||column2||','||column3 ....<br />from some_table where condition<br />spool off<br />#這裡可以添加多個查詢,且每個查詢可輸出到不同的檔案,如下面注釋<br />#spool /tmp/some_tmp.csv<br />#select column1||','||column2||','||column3 ....<br />#from some_table where condition<br />#spool off<br />set markup HTML off<br />quit<br />!!<br />#計算記錄總行數,如果是0則不發郵件<br />NUM=`cat /tmp/some_tmp.csv | wc -l`<br />if[ $NUM -gt 0 ]<br />then<br />#先寫excel的每列的title<br />echo 'Title_Of_Column1, Title_Of_Column2, ....Title_Of_ColumnN' > /tmp/attachement.csv<br />#資料本文<br />cat /tmp/some_tmp.csv >> /tmp/attachement.csv<br />#發郵件<br />(<br />echo "From: user@somehost.com"<br />echo "To: user1@somehost.com"<br />echo "MIME-Version: 1.0"<br />echo "Content-Type: multipart/mixed;"<br />echo ' boundary="A_Boundary_Name_You_Can_Change_It"'<br />echo "Subject: Subject"<br />echo ""<br />echo ""<br />#附件分隔字元,上面的boundary前面加--<br />echo "--A_Boundary_Name_You_Can_Change_It"<br />echo 'Content-Transfer-Encoding: x-uuencode'<br />echo 'Content-Type: text/csv;'<br />echo ' name="attachement.csv"'<br />echo 'Content-Disposition: attachment;'<br />echo ' filename="attachement.csv"'<br />echo ""<br />uuencode /tmp/attachement.csv attachement.csv<br />echo "--A_Boundary_Name_You_Can_Change_It"<br />#附件結束<br />) | mailx -t<br />fi<br />#刪除臨時檔案<br />rm -f /tmp/*.csv<br />exit 0<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.