需要 sudo 密码的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976362/
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
Bash scripts requiring sudo password
提问by Thijs Koerselman
I'm creating a Bash installer script which compiles and installs some libraries for both OSX and Linux. Because some commands in my script ("make install", "apt-get install", "port install", etc) require sudo, I need the user to supply the password.
我正在创建一个 Bash 安装程序脚本,它为 OSX 和 Linux 编译和安装一些库。因为我的脚本中的某些命令(“make install”、“apt-get install”、“port install”等)需要 sudo,所以我需要用户提供密码。
Currently the user gets asked for the password whenever the first sudo command is about to execute, but because this is often after a compile stage, there is always some time between starting the script and having to enter the password.
目前,每当第一个 sudo 命令即将执行时,用户都会被要求输入密码,但因为这通常是在编译阶段之后,所以在启动脚本和必须输入密码之间总是有一些时间。
I would like to put the password entry + check at the beginning of the script. Also I am curious if this is really an ok way of installing system libraries.
我想将密码输入 + 检查放在脚本的开头。我也很好奇这是否真的是安装系统库的好方法。
Alternatively I could install the libraries in a local sandbox location which doesn't require sudo, but then I'll have to tell apt-get and macports where to install their libraries other then the default /usr/local/ and /opt/local, and I'm not sure how to do that nor if that's a clever idea at all.
或者,我可以将库安装在不需要 sudo 的本地沙箱位置,但是我必须告诉 apt-get 和 macports 在哪里安装它们的库,而不是默认的 /usr/local/ 和 /opt/local ,我不知道该怎么做,也不知道这是否是一个聪明的主意。
回答by Aaron Digulla
To get the password, just put sudo echo "Thanks."at the start of the script.
要获取密码,只需放在sudo echo "Thanks."脚本的开头即可。
But I would prefer this solution:
但我更喜欢这个解决方案:
if [[ $UID != 0 ]]; then
echo "Please run this script with sudo:"
echo "sudo if [[ ! $(sudo echo 0) ]]; then exit; fi
$*"
exit 1
fi
回答by Travis R
For those who don't want to elevate the entire script (to limit risks by only using sudo within the script where needed) the first part of the accepted answer sudo echo "Thanks"works but won't respond to sudo password failure by exiting the script. To accomplish this, scripts that include sudo commands and want to ensure sudo access before it's used could start with
对于那些不想提升整个脚本的人(通过仅在需要时在脚本中使用 sudo 来限制风险),已接受答案的第一部分sudo echo "Thanks"有效,但不会通过退出脚本来响应 sudo 密码失败。为了实现这一点,包含 sudo 命令并希望在使用之前确保 sudo 访问的脚本可以从
function checkSudo() {
if ((EUID != 0)); then
echo "Granting root privileges for script ( $SCRIPT_NAME )"
if [[ -t 1 ]]; then
sudo "[ $(whoami) == "root" ] || exit
" "$@"
else
exec 1>output_file
gksu "##代码##" "$@"
fi
exit
fi
}
The caveat is that you are relying on the existence of a sudoers timeout that will last the duration of your script to suppress the rest of the prompts.
需要注意的是,您依赖于 sudoers 超时的存在,该超时将持续您的脚本持续时间以抑制其余提示。
回答by Mike Q
Another way to go about it :
另一种方法:
##代码##回答by Paul Back
Maybe a bit easier to read:
也许更容易阅读:
##代码##
