2013-10-25 158 views
1

我需要根據提供電子郵件地址的SQL輸出設置BASH變量。作爲測試我做:根據SQL輸出設置BASH變量

email=$(echo "SELECT email FROM adr" | sqlplus $user/[email protected]$db)

echo $email顯示:

SQL*Plus: Release 8.1.7.0.0 - Production on Fri Oct 25 10:45:06 2013 (c) Copyright 2000 Oracle Corporation. All rights reserved. Connected to: Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production JServer Release 8.1.7.4.0 - Production SQL> 2 Disconnected from Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production JServer Release 8.1.7.4.0 - Production

所以我想我需要一些微調,以剛剛獲得電子郵件地址的?

手動SELECT如下顯示完整的輸出:

SQL> SELECT email FROM adr; 

EMAIL 
-------------------------------------------------------------------------------- 
[email protected] 

什麼是搶的e-mail地址的最簡單的方法?

+0

http://stackoverflow.com/questions/15663763/getting-sqlresult-into-bash-variable-without-oracle-header-footer?rq= 1 – Mat

回答

2

使用-s(靜音模式)。此外,關閉列標題和反饋如下所示:

email=$(sqlplus -s $user/[email protected]$db << EOF 
set serveroutput on 
set heading off 
set feedback off 
SELECT email FROM adr; 
exit; 
EOF 
) 
+0

謝謝,但電子郵件地址似乎有一個行分隔符,因此它打破了腳本。解決這個問題的最好方法是什麼? – user2656114

+0

使用'tr'去除換行符。將第一行更改爲:'email = $(sqlplus -s/nolog $ user/$ pass @ $ db << EOF | tr -d'\ n'' – dogbane

+0

謝謝,我可以問一下'/ NOLOG'的用途嗎? – user2656114