如何使用 su 以该用户身份执行 bash 脚本的其余部分?
我编写了一个脚本,它采用一个由用户名和项目连接而成的字符串作为参数。该脚本应该根据项目字符串切换 (su) 到用户名,cd 到特定目录。
我基本上想做:
su $USERNAME;
cd /home/$USERNAME/$PROJECT;
svn update;
问题是,一旦我做了 su... 它就在那里等待。这是有道理的,因为执行流程已经传递到切换到用户。一旦我退出,其余的事情就会执行,但它不会按预期工作。
我在 svn 命令前面添加了 su,但该命令失败了(即它没有在所需的目录中更新 svn)。
如何编写一个允许用户切换用户并调用 svn(以及其他功能)的脚本?
I've written a script that takes, as an argument, a string that is a concatenation of a username and a project. The script is supposed to switch (su) to the username, cd to a specific directory based upon the project string.
I basically want to do:
su $USERNAME;
cd /home/$USERNAME/$PROJECT;
svn update;
The problem is that once I do an su... it just waits there. Which makes sense since the flow of execution has passed to switching to the user. Once I exit, then the rest of the things execute but it doesn't work as desired.
I prepended su to the svn command but the command failed (i.e. it didn't update svn in the directory desired).
How do I write a script that allows the user to switch user and invoke svn (among other things)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更简单:使用
sudo
运行 shell 并使用 heredoc 来喂它命令。(最初在 SuperUser 上回答)
Much simpler: use
sudo
to run a shell and use a heredoc to feed it commands.(answer originally on SuperUser)
诀窍是使用“sudo”命令而不是“su”
您可能需要将其添加
到 /etc/sudoers 文件
并将脚本更改为:
其中 username2 是您要运行 SVN 命令的用户,而 username1 是运行脚本的用户。
如果您需要多个用户来运行此脚本,请使用
%groupname
而不是用户名1The trick is to use "sudo" command instead of "su"
You may need to add this
to your /etc/sudoers file
and change your script to:
Where username2 is the user you want to run the SVN command as and username1 is the user running the script.
If you need multiple users to run this script, use a
%groupname
instead of the username1这是另一种方法,在我的情况下更方便(我只是想放弃 root 权限并从受限用户执行脚本的其余部分):您可以使脚本从正确的用户重新启动。这种方法比使用
sudo
或su -c
与“嵌套脚本”更具可读性。假设它最初是作为 root 启动的。那么代码将如下所示:Here is yet another approach, which was more convenient in my case (I just wanted to drop root privileges and do the rest of my script from restricted user): you can make the script restart itself from the correct user. This approach is more readable than using
sudo
orsu -c
with a "nested script". Let's suppose it is started as root initially. Then the code will look like this:您需要将所有不同用户的命令作为自己的脚本来执行。如果它只是一个或几个命令,那么内联应该可以工作。如果有很多命令,那么最好将它们移动到自己的文件中。
You need to execute all the different-user commands as their own script. If it's just one, or a few commands, then inline should work. If it's lots of commands then it's probably best to move them to their own file.
使用如下所示的脚本在另一个用户下执行脚本的其余部分或部分内容:
Use a script like the following to execute the rest or part of the script under another user:
使用
sudo
代替编辑:正如Douglas指出的,您不能在
sudo
中使用cd
,因为它不是外部命令。您必须在子 shell 中运行命令才能使cd
工作。系统可能会要求您输入该用户的密码,但仅输入一次。
Use
sudo
insteadEDIT: As Douglas pointed out, you can not use
cd
insudo
since it is not an external command. You have to run the commands in a subshell to make thecd
work.You may be asked to input that user's password, but only once.
无法在 shell 脚本中更改用户。其他答案中描述的使用 sudo 的解决方法可能是您最好的选择。
如果您疯狂地想以 root 身份运行 perl 脚本,您可以使用
$<; $( $> $)
保存真实/有效 uid/gid 的变量,例如:It's not possible to change user within a shell script. Workarounds using sudo described in other answers are probably your best bet.
If you're mad enough to run perl scripts as root, you can do this with the
$< $( $> $)
variables which hold real/effective uid/gid, e.g.:这对我有用,
我将我的“配置”从“启动”中分离出来。
然后在我的start_env.sh中
This worked for me
I split out my "provisioning" from my "startup".
then in my start_env.sh
受到 @MarSoft 的想法的启发,但我更改了如下行:
我使用了
sudo
允许无密码执行脚本。如果您想输入用户密码,请删除sudo
。如果不需要环境变量,请从 sudo 中删除-E
。/usr/bin/bash -l
确保在初始化的环境中执行profile.d
脚本。Inspired by the idea from @MarSoft but I changed the lines like the following:
I have used
sudo
to allow a password less execution of the script. If you want to enter a password for the user, remove thesudo
. If you do not need the environment variables, remove-E
from sudo.The
/usr/bin/bash -l
ensures, that theprofile.d
scripts are executed for an initialized environment.