2013-01-21 36 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