的bash脚本需要sudo的密码 [英] Bash scripts requiring sudo password

查看:121
本文介绍了的bash脚本需要sudo的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个猛砸安装脚本编译和安装一些图书馆为OSX和Linux操作系统。因为在我的脚本的一些命令(make install的,和apt-get安装,端口设置​​等)需要sudo的,我需要的用户提供密码。

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.

目前的用户被要求无论何时所述第一须藤命令即将执行,但因为这是通常编译阶段后,总有启动脚本和具有输入密码之间的一些时间的密码。

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.

我想将密码输入+在脚本的开始检查。我也很好奇,如果这真的是安装系统库的确定方法。

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.

我也可以安装在图书馆不需要须藤本地沙箱的位置,但我得告诉apt-get和MacPorts的安装位置等他们的图书馆则缺省的/ usr / local /,并且/选择/地方,我不知道该怎么做,也不如果这是一个聪明的主意。

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.

推荐答案

要获取密码,只是把须藤回声谢谢。在脚本的开始。

To get the password, just put sudo echo "Thanks." at the start of the script.

不过,我想preFER此解决方案:

But I would prefer this solution:

if [[ $UID != 0 ]]; then
    echo "Please run this script with sudo:"
    echo "sudo $0 $*"
    exit 1
fi

这篇关于的bash脚本需要sudo的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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