bash 将用户更改为 oracle 并在 shell 脚本中运行 sql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14437047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change user to oracle and run sql in a shell script
提问by Halit Sakca
I am a root user and in a shell script I would like to change user to oracle than run a sql script, I tried following;
我是 root 用户,在 shell 脚本中,我想将用户更改为 oracle 而不是运行 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
it gives me following error;
它给了我以下错误;
./deneme2.sh: syntax error at line 12: `end of file' unexpected
Can you please let me know what might be the problem?
你能告诉我可能是什么问题吗?
Thanks, Halit
谢谢,哈利特
回答by lanes
When using here documents the closing string MUST be at the beginning of the line!
使用 here 文档时,结束字符串必须位于行首!
Try
尝试
#!/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
回答by Suku
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s..........
You don't need EOF here. Execute your sqlpluscommand like above. In this case your oracleuser must be a sudouser.
这里不需要EOF。sqlplus像上面一样执行你的命令。在这种情况下,您的oracle用户必须是sudo用户。
If oracleis a normal user
如果oracle是普通用户
su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........."
A little more about sucommand (From man page):
关于su命令的更多信息(来自手册页):
The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly. Additional arguments may be provided after the username, in which case they are supplied to the user's login shell. In particular, an argument of -c will cause the next argument to be treated as a command by most command interpreters. The command will be executed by the shell specified in /etc/passwd for the target user.
su 命令用于在登录会话期间成为另一个用户。在没有用户名的情况下调用,su 默认成为超级用户。可选参数 - 可用于提供类似于用户直接登录时所期望的环境。可以在用户名之后提供其他参数,在这种情况下,它们将提供给用户的登录 shell。特别是, -c 的参数将导致大多数命令解释器将下一个参数视为命令。该命令将由 /etc/passwd 中为目标用户指定的 shell 执行。
回答by dani herrera
You can use su. Remember get environment with su -:
您可以使用su. 请记住使用以下命令获取环境su -:
COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... "
su - oracle -c $COMMAND
A nice sample oracle-base site, Automating Database Startup and Shutdown on Linux Post:
一个不错的基于oracle 的示例 站点,Linux Post 上的自动数据库启动和关闭:
case "" 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
;;

