2013-01-21 34 views
0

我是root用戶,並且在shell腳本中,我想將用戶更改爲oracle,而不是運行sql腳本,我嘗試了下面的內容;將用戶更改爲oracle並在shell腳本中運行sql

#!/bin/sh 

portStatus=`lsof -ni:5060` 
if [ ${#portStatus} -ne 0 ] 
    then 
    sudo -u oracle << EOF 
    /oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql; 
    EOF 
    else 
    exit 
fi 

它給了我下面的錯誤;

./deneme2.sh: syntax error at line 12: `end of file' unexpected 

您能否讓我知道可能是什麼問題?

感謝, 哈利特

回答

0
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s.......... 

你不需要EOF這裏。執行您的sqlplus命令如上。在這種情況下,您的oracle用戶必須是sudo用戶。

如果oracle是一個普通用戶

su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........." 

,稍微介紹一下su命令(手冊頁):

su命令來成爲一個登錄會話過程中的其他用戶。 在沒有用戶名的情況下調用,su默認爲成爲超級用戶。 可選參數 - 可用於提供類似於用戶期望用戶直接登錄的環境的類似 的環境。 在用戶名後面可能會提供其他參數,在這種情況下, 將被提供給用戶的登錄shell。特別是,-c的參數 將導致大多數命令解釋程序將下一個參數視爲命令 。該命令將由目標用戶在/ etc/passwd中指定的 shell執行。

+0

你用'蘇ISO sudo'是什麼意思? – Suku

+0

親愛的suku,謝謝我用'su' iso'sudo'嘗試過,但它工作,但是我想知道我是否可以在默認模式下運行它,因爲每當我運行它時,oracle登錄文本出現哪些困擾我:( ' !/ bin/ksh ..... su - oracle -c「/ oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql」; .....' --->'警告:如果您沒有授權,請勿使用計算機 警告:您已收到警告 採購/etc/.profile-EIS ................. –

+0

這意味着我在腳本中使用了'su'命令而不是'sudo'。 –

0

您可以使用su。記住得到環境su -

COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... " 
su - oracle -c $COMMAND 

一個很好的樣本oracle-base site, Automating Database Startup and Shutdown on Linux Post

case "$1" in 
    'start') 
     # Start the Oracle databases: 
     # The following command assumes that the oracle login 
     # will not prompt the user for any values 
     su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" 
     su - $ORA_OWNER -c $ORA_HOME/bin/dbstart 
     touch /var/lock/subsys/dbora 
     ;; 
+0

謝謝@danihp我很欣賞 –

1

當使用文檔在這裏閉幕字符串必須在該行的開始!

嘗試

#!/bin/sh 

portStatus=`lsof -ni:5060` 
if [ ${#portStatus} -ne 0 ] 
    then 
    sudo -u oracle << EOF 
    /oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql; 
EOF 
    else 
    exit 
fi