简体   繁体   English

使用sudo su-team并仅在此sudo访问下执行rest命令的shell脚本

[英]shell scripting that uses sudo su - team and perform rest command under this sudo access only

#!/bin/bash
sudo su - team  //// this will change user to team
<somecommnand>

here i have some command like sqlplus that run under this privilege only . 在这里,我有一些像sqlplus这样的命令只能在此特权下运行。

but ,this script first asked for password then i give password but it not run rest of command like . 但是,此脚本首先要求输入密码,然后我提供密码,但它没有像这样运行其余命令。

How to write script that use sudo su and run all command in script below it. 如何编写使用sudo su的脚本并在其下面的脚本中运行所有命令。

You have several options. 您有几种选择。

  1. You can just move the sudo command outside of your script, and have people call it like this: 您可以将sudo命令移出脚本,然后让人们这样称呼它:

     sudo -u team /path/to/your/script 
  2. You can put the sudo command in one script and the actual commands you want to run with elevated privileges in another script. 您可以将sudo命令放在一个脚本中,而要以提升的特权运行的实际命令放在另一个脚本中。 That is, people run your_script , which looks like: 也就是说,人们运行your_script ,如下所示:

     #!/bin/sh sudo -u team /path/to/your_script.real 

    And your_script.real contains the commands that you want to run with elevated privileges. 而且your_script.real包含您要以提升的特权运行的命令。

  3. If you really want everything in the same script and you don't mind being a little too clever, you can have the script check re-execute itself using sudo like this: 如果您确实希望所有内容都在同一脚本中,并且您不介意有点太聪明,则可以使用sudo使脚本检查重新执行,如下所示:

     #!/bin/sh TEAM_UID=500 # replace with correct uid if [ "$UID" != $TEAM_UID ]; then exec sudo -u team $0 fi 
  4. And you can also feed commands to a shell from stdin , like this: 您还可以从stdin向shell馈送命令,如下所示:

     #!/bin/sh sudo su - team <<'EOF' echo my uid is $UID EOF 

You could try having all of the commands on the same line and seperate them with ';'. 您可以尝试将所有命令放在同一行中,并用';'分隔。

eg. 例如。 'sudo su; 'sudo su; somecommand; 命令 another command' 另一个命令”

Could also run that script as the intended user. 也可以以目标用户身份运行该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM