我如何使用su执行bash脚本作为用户的休息吗? [英] How do I use su to execute the rest of the bash script as that user?

查看:140
本文介绍了我如何使用su执行bash脚本作为用户的休息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,它作为一个参数,一个字符串,它是一个用户名和项目的串联。该脚本应该(SU)切换到用户名,cd到基于项目的字符串一个特定的目录。

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.

基本上,我想做的事:

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.

我prepended苏的svn命令,但命令失败(即它没有所需的目录更新SVN)。

I prepended su to the svn command but the command failed (i.e. it didn't update svn in the directory desired).

我如何编写一个脚本,允许用户切换用户,并调用SVN(除其他事项外)?

How do I write a script that allows the user to switch user and invoke svn (among other things)?

推荐答案

的技巧是使用命令命令,苏

The trick is to use "sudo" command instead of "su"

您可能需要添加此

username1 ALL=(username2) NOPASSWD: /path/to/svn

你的/ etc / sudoers文件

to your /etc/sudoers file

和改变你的脚本:

sudo -u username2 -H sh -c "cd /home/$USERNAME/$PROJECT; svn update" 

在哪里USERNAME2就是你要运行的SVN命令和USERNAME1在用户运行脚本的用户。

Where username2 is the user you want to run the SVN command as and username1 is the user running the script.

如果您需要多个用户运行此脚本,使用%组名而不是USERNAME1

If you need multiple users to run this script, use a %groupname instead of the username1

这篇关于我如何使用su执行bash脚本作为用户的休息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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