如何在 Xcode 4 中的运行脚本构建阶段使用 sudo?
我需要使用 sudo 在 Xcode 4 的运行脚本构建阶段中的脚本内执行命令。然而,编译器抱怨:
sudo:不存在 tty 且未指定 Askpass 程序
有人对这个问题有一个聪明的解决方案吗?
I need use execute a command inside of a script in a Run Script build phase in Xcode 4 using sudo. However, the compiler complains:
sudo: no tty present and no askpass program specified
Anyone have a clever solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一种解决方案是将 sudo 密码放置在可执行 shell 脚本中,如下所示:
该 shell 脚本可能称为password.sh
然后,设置环境变量 SUDO_ASKPASS=password.sh
设置完成后,可以将 -A 选项传递给须藤。此选项使用 ASKPASS 程序来获取 sudo 密码。 ASKPASS 程序只需将密码写入 stdout。
例如,
这显然是一个相当不安全的解决方案,但它确实有效。
One solution is to place the sudo password in an executable shell script like the following:
This shell script might be called password.sh
Then, setup the environment variable SUDO_ASKPASS=password.sh
Once this is setup, the -A option can be passed to sudo. This option uses the ASKPASS program to obtain the sudo password. The ASKPASS program need only write the password to stdout.
So, for example,
This is obviously a rather insecure solution, but it does work.
尚未提出的两个想法,这两个想法可能都比当前接受的答案更好/更安全:
第一个选项是将需要作为 root 运行的脚本部分放在脚本文件中(.sh 或某事),然后将其设置为 root:
chmod go-w,+sx scriptfile
、sudo chown root scriptfile
。这意味着脚本将自动以 root 身份运行,从而避免您需要进行身份验证才能运行它(只是为了更改它)。只要其操作不受用户输入的影响,这应该是相当安全的。 (当然,如果您制作一个脚本,该脚本接受输入参数并删除它或运行它,或者用它执行大多数其他操作,那么这是不安全的。)第二个选择是使用 applescript (可能通过 osascript)。 Applescript 允许您使用管理员权限
执行 shell 脚本“sudo 命令在此处”
,这将弹出一个图形对话框,要求输入密码。第一个选项适用于自动化环境,但它可能无法很好地处理(例如)签入 SCM 或发送给其他用户的情况。第二个选项可以更好地工作,但每次都需要输入密码,因此对于自动构建脚本来说效果不佳。
Two ideas that haven't been suggested yet, both of which are probably better/safer than the currently accepted answer:
First option would be to put the part of the script that needs to be run as root in a script file (.sh or something), and then make it setuid as root:
chmod go-w,+sx scriptfile
,sudo chown root scriptfile
. This means that script will automatically run as root, which avoids you needing to authenticate to run it (just to change it). As long as its operation isn't subject to user input, this should be quite safe. (Of course, if you make a script that takes an input argument and deletes it or runs it, or does most anything else with it, that would not be safe.)Second option would be to use applescript (possibly via osascript). Applescript allows you to
do shell script "sudo command goes here" with administrator privileges
, which will pop up a graphical dialog asking for a password.The first of these options would be good for an automated environment, though it might not deal well with (for example) being checked into an SCM, or being sent to another user. The second option would work better with that, but requires a password input every time, so doesn't work as well for an automated build script.
此问题的另一个解决方案是修改 sudoers 文件并将您的帐户添加到其中,并声明永远不会要求您提供 sudo 密码。要完成此操作相当简单:
运行:
在用户权限规范部分中添加一行,如下所示
当然,这可能是一件危险的事情,所以要小心。我建议在走这条路线之前阅读 sudoers 和 visudo 的手册页。
Another solution to this problem is to modify sudoers file and add your account to it and state that you should never be asked for the sudo password. To accomplish this is fairly straightforward:
run:
In the User privilege specification section add a line that looks like
Of course, this can be a dangerous thing to do, so be careful. I would suggest reading the man page for sudoers and visudo before going this route.
经过大量搜索,我找到了以下解决方案。
https: //forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112
摘要
After much searching I found the following solution.
https://forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112
Summary
您可以使用以下命令直接以管理员身份运行命令(将
echo YourCommandHere > /tmp/hello
更改为您的命令):或者使用以下命令在源目录中运行脚本:
这会运行脚本并记录日志它输出到 /tmp/build-log 和 /tmp/build-log.err
有关脚本中有用的变量,请参阅 https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6
You can either run commands directly as a administrator with the following (changing
echo YourCommandHere > /tmp/hello
to your command):Or run a script in your source directory using:
This runs the script and logs it output to /tmp/build-log and /tmp/build-log.err
For useful variables in the script see https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6
您还可以使用 sudo 执行 XCode,将项目作为终端的参数,如下所示:
这是我能想到的最简单的解决方案,但可能存在一些缺点,因为您将以 root 身份执行 XCode。
You can also execute XCode giving it the project as parameter from the Terminal using sudo like this:
This is the easiest solution I could think of, but there may be some drawbacks, since you would be executing XCode as root.
无需在任何地方写下 sudo 密码。只需打开一个终端窗口并输入
一旦你输入了密码,它会在一段时间内有效 - 不确定多久 - 并且 Xcode 生成的 shell 将继承此权限。
如果稍后再次收到“no tty present”消息,只需重复该过程
No need to write your sudo password anywhere. Just open a terminal window and type
Once you've typed your password, it will be good for a while - not sure how long - and the shell spawned by Xcode will inherit this permission.
If you get the "no tty present" message again later, just repeat the procedure