在BASH Shell中以文件中的另一个用户名执行多个命令 [英] Executing multiple commands under as another username within a file in BASH shell

查看:50
本文介绍了在BASH Shell中以文件中的另一个用户名执行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文件中执行sqlplus命令.该文件应该能够sudo切换到oracle用户并运行所需的命令.oracle用户将需要首先从数据库中获取带有参数的文件,然后才能从sqlplus调用sql文件.

I am attempting to execute a sqlplus command within a file. The file should be able to sudo switch into the oracle user and run the commands needed. The oracle user will need to first source a file with the parameters for the database, then be able to call a sql file from sqlplus.

我的脚本是

#!/bin/bash
sudo -iu user oracle -c `
cd /tmp;
. .sourceme;
sqlplus user/password << EOF;
SPOOL logfile.lst;
@runme.sql;
spool off;
exit;
EOF;`

无论我是否使用sudo运行scrupt,它都不会出错.它唯一会返回的是sudo的用法

Whether I run the scrupt with sudo or not, it won't give an error. The only thing it will return is the usage for sudo

usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-p prompt]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u username|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] file ...

我认为这意味着我的语法可能不正确.但是,如果不将命令完全放在一行上,则不确定如何修复语法.我尝试在命令周围使用双引号,删除分号,并更改坟墓符号以改为使用括号.这些都不产生任何结果.尝试将其作为一个衬板,并删除括号和坟墓标记,这将为我提供使用信息以及一堆

I figure that this means that my syntax is probably incorrect. However, without putting the command entirely on one line, I'm not sure how to fix the syntax. I've tried using double quotes around the command, removing the semicolon, and changing the grave symbol to use parenthesis instead. None of these produce any results. Trying it as a one liner and removing the parenthesis and grave marks gives me the usage information as well as a bunch of

./background: line 2: spool: command not found

推荐答案

尝试使用 heredoc :

#!/bin/bash
sudo -iu user oracle <<XXX
cd /tmp;
. .sourceme;
sqlplus user/password << EOF;
SPOOL logfile.lst;
@runme.sql;
spool off;
exit;
EOF;
XXX

这篇关于在BASH Shell中以文件中的另一个用户名执行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆