在执行shell脚本简单的命令没有sudo [英] Execute single command in shell script without sudo
问题描述
我有一个使用sudo运行大多数脚本需要它一个简单的shell脚本,但是在脚本中的命令之一是家酿安装,不能使用sudo。
I have a simple shell script that is run with sudo as most of the script requires it, yet one of the commands in the script is a Homebrew install, which cannot be executed with sudo..
所以,我的问题是执行使用sudo一个shell脚本我怎么执行命令子作为当前用户,然后继续使用sudo脚本的剩余部分时。
So, my question is when executing a shell script with sudo how do I execute sub commands as the current user and then continue the remainder of the script with sudo.
提示用户输入他的密码又是不是真的实用的脚本需要很长的执行,将需要等待5-10分钟的提示。
Prompting the user to enter his password again is not really practical as the script takes really long to execute and would require waiting 5-10 min for the prompt.
推荐答案
最简单的方法是从脚本中运行通过须藤
的子命令。用户ID与可以通过$ SUDO_USER获得运行(看的输出须藤ENV
)
The easiest way is to run the subcommand via sudo
from within the script. The user id to run with can be obtained by $SUDO_USER (look at the output of sudo env
):
sudo -u $SUDO_USER ./exec_as_normal_user.sh
这篇关于在执行shell脚本简单的命令没有sudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!