bash 如何在sudo中运行两个命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5560442/
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
how to run two commands in sudo?
提问by Radek
Is there any way how I can run two Db2 commands from a command line? (They will be called from a PHP exec
command.)
有什么办法可以从命令行运行两个 Db2 命令?(它们将从 PHPexec
命令调用。)
db2 connect to ttt
(note that we need to have the connection live for the second commanddb2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'
db2 connect to ttt
(请注意,我们需要为第二个命令启用连接db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'
I tried this:
我试过这个:
sudo -su db2inst1 db2 connect to ttt; db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'
The first command finishes correctly but the second one fails with the error message SQL1024N A database connection does not exist. SQLSTATE=08003
第一个命令正确完成,但第二个命令失败并显示错误消息 SQL1024N A database connection does not exist. SQLSTATE=08003
Note that I need to run this as php user. The command sudo -u db2inst1 id
as phpuser gives me correct output.
请注意,我需要以 php 用户身份运行它。sudo -u db2inst1 id
作为php用户的命令给了我正确的输出。
回答by Jason
For your command you also could refer to the following example:
对于您的命令,您还可以参考以下示例:
sudo sh -c "whoami; whoami"
sudo sh -c "whoami; whoami"
回答by wjl
sudo can run multiple commands via a shell, for example:
sudo 可以通过一个 shell 运行多个命令,例如:
$ sudo -s -- 'whoami; whoami' root root
Your command would be something like:
您的命令将类似于:
sudo -u db2inst1 -s -- "db2 connect to ttt; db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'"
If your sudo version doesn't work with semicolons with -s (apparently, it doesn't if compiled with certain options), you can use
如果您的 sudo 版本不能使用带 -s 的分号(显然,如果使用某些选项编译,则不能使用),您可以使用
sudo -- sh -c 'whoami; whoami'
instead, which basically does the same thing but makes you name the shell explicitly.
相反,它基本上做同样的事情,但让你明确命名外壳。
回答by Samer Atiani
I usually do:
我通常这样做:
sudo bash -c 'whoami; whoami'
回答by Neboj?a
If you would like to handle quotes:
如果您想处理报价:
sudo -s -- <<EOF
id
pwd
echo "Done."
EOF
回答by Cas
An alternative using eval
so avoiding use of a subshell:
使用eval
so 避免使用子shell的替代方法:
sudo -s eval 'whoami; whoami'
Note: The other answers using sudo -s
fail because the quotes are being passed on to bash and run as a single command so need to strip quotes with eval. eval
is better explained is this SO answer
注意:使用sudo -s
失败的其他答案是因为引号被传递给 bash 并作为单个命令运行,因此需要使用 eval 去除引号。eval
更好地解释是这个SO 答案
Quoting within the commands is easier too:
在命令中引用也更容易:
$ sudo -s eval 'whoami; whoami; echo "end;"'
root
root
end;
And if the commands need to stop running if one fails use double-ampersands instead of semi-colons:
如果命令在失败时需要停止运行,请使用双与号而不是分号:
$ sudo -s eval 'whoami && whoamit && echo "end;"'
root
/bin/bash: whoamit: command not found
回答by user2597485
The -s
option didn't work for me, -i
did.
该-s
选项对我不起作用,-i
确实如此。
Here is an example of how I could update the log size from my bash:
这是我如何从 bash 更新日志大小的示例:
sudo -u [user] -i -- sh -c 'db2 connect to [database name];db2 update db cfg for [database name] using logsecond 20;db2 update db cfg for [database name] using logprimary 20;'
回答by Moshe Simantov
On the terminal, type:
在终端上,输入:
$ sudo bash
Then write as many commands as you want. Type exit
when you done.
然后根据需要编写任意数量的命令。键入exit
当你完成。
If you need to automate it, create a script.sh
file and run it:
如果您需要自动化,请创建一个script.sh
文件并运行它:
$ sudo ./script.sh
回答by Eugene Chow
On a slightly-related topic, I wanted to do the same multi-command sudo via SSH but none of the above worked.
在一个稍微相关的主题上,我想通过 SSH 执行相同的多命令 sudo 但以上都没有奏效。
For example on Ubuntu,
例如在 Ubuntu 上,
$ ssh host.name sudo sh -c "whoami; whoami"
[sudo] password for ubuntu:
root
ubuntu
The trick discovered hereis to double-quote the command.
这里发现的技巧是双引号命令。
$ ssh host.name sudo sh -c '"whoami; whoami"'
[sudo] password for ubuntu:
root
root
Other options that also work:
其他也有效的选项:
ssh host.name sudo sh -c "\"whoami; whoami\""
ssh host.name 'sudo sh -c "whoami; whoami"'
In principle, double-quotes are needed because I think the client shell where SSH is run strips the outermost set of quotes. Mix and match the quotes to your needs (eg. variables need to be passed in). However YMMV with the quotes especially if the remote commands are complex. In that case, a tool like Ansible will make a better choice.
原则上,需要双引号,因为我认为运行 SSH 的客户端 shell 会去除最外层的引号。根据您的需要混合和匹配引号(例如,需要传入变量)。但是 YMMV 带有引号,尤其是在远程命令很复杂的情况下。在这种情况下,像 Ansible 这样的工具将是一个更好的选择。
回答by shards
If you know the root password, you can try
如果你知道root密码,你可以试试
su -c "<command1> ; <command2>"
回答by arberg
The above answers won't let you quote inside the quotes. This solution will:
上面的答案不会让你在引号内引用。该解决方案将:
sudo -su nobody umask 0000 \; mkdir -p "$targetdir"
Both the umask command and the mkdir-command runs in with the 'nobody' user.
umask 命令和 mkdir-command 都以“nobody”用户运行。