macos 在 OS X 上以管理员权限运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1371092/
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
Running script with admin permissions on OS X
提问by ScottN
I've tried my best to find out a solution with the many script questions on Stack Overflow and the internet, but I can't seem to find the solution I need.
我已经尽力在 Stack Overflow 和互联网上找到许多脚本问题的解决方案,但我似乎无法找到我需要的解决方案。
What I want to do is create a more automated and less clicking solution to remove all the Mobile cached user accounts on a system. I've been logging in and manually going to user accounts and removing the users one at a time by clicking the "-" button, then clicking "Delete Immediately" for the user data. This works, but is time consuming and I have better things to do with my time. So I knew there had to be a way to do this with a script.
我想要做的是创建一个更自动化且点击次数更少的解决方案,以删除系统上的所有 Mobile 缓存用户帐户。我一直在登录并手动转到用户帐户并通过单击“-”按钮一次删除一个用户,然后单击用户数据的“立即删除”。这有效,但很耗时,而且我的时间有更好的事情要做。所以我知道必须有一种方法可以用脚本来做到这一点。
I ran across this code:
我遇到了这个代码:
for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print }' | tr '/n' ' '`; do
dscl . -delete /Users/$cuser
done
If I run this in terminal I get permission errors. So I figured I need to run it with sudo. So I started looking into creating AppleScripts to run the script, but I can't seem to find the right way to do it.
如果我在终端中运行它,我会收到权限错误。所以我想我需要用 sudo 运行它。所以我开始研究创建 AppleScripts 来运行脚本,但我似乎找不到正确的方法来做到这一点。
Any ideas? By the way, I'm new to scripting on the Mac, so please comment your code so I know whats happening, and so I don't just run some script code without know what it'll do. :)
有任何想法吗?顺便说一句,我是在 Mac 上编写脚本的新手,所以请评论您的代码,以便我知道发生了什么,所以我不会在不知道它会做什么的情况下运行一些脚本代码。:)
Thanks
谢谢
回答by Chealion
To perform a shell script with sudo
or administrator privileges append with administrator privileges
to the end of your do shell script
line. For example:
要使用sudo
或管理员权限执行 shell 脚本,请附加with administrator privileges
到您的do shell script
行尾。例如:
do shell script "/path/to/script/file.sh" user name "adminusershortname" password "password" with administrator privileges
You can find more on Apple's technote dealing with do shell script
您可以在 Apple 的技术说明上找到更多信息 do shell script
That said, saving this as a shell script and running the shell script using sudo would work just as well.
也就是说,将其保存为 shell 脚本并使用 sudo 运行 shell 脚本也能正常工作。
#! /bin/sh
for cuser in `/usr/bin/dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print }' | tr '/n' ' '`; do
/usr/bin/dscl . -delete /Users/$cuser
done
Save it as say removeUser.sh, use chmod
to set it as executable (chmod 755
) and then run it (sudo ./removeUser.sh
)
将其保存为说removeUser.sh,用于chmod
将其设置为可执行文件(chmod 755
)然后运行它(sudo ./removeUser.sh
)
回答by Jesse L
You can do this by editing your system's sudoers file. This will allow the account you use to run this script (via cron, etc.) the ability to run sudo without a password.
您可以通过编辑系统的 sudoers 文件来完成此操作。这将允许您用来运行此脚本的帐户(通过 cron 等)无需密码即可运行 sudo。
To edit the sudoers file you use visudo, but it must be run with admin permission. Try:
要编辑 sudoers 文件,您可以使用 visudo,但它必须以管理员权限运行。尝试:
$ sudo visudo
Add a line like the following to the end of the file, replacing user_namewith the user who will run your script. Note, use tabs between each field.
在文件末尾添加如下一行,将user_name替换为将运行您的脚本的用户。请注意,在每个字段之间使用制表符。
user_name ALL=(ALL) NOPASSWD:ALL
Now user_name should be able to type sudo and will not be prompted for a password.
现在 user_name 应该能够输入 sudo 并且不会提示输入密码。
Also note that visudo is a text editor that mirrors the vi editor and uses the same commands as vi.
另请注意,visudo 是一个文本编辑器,它反映了 vi 编辑器并使用与 vi 相同的命令。
回答by Matt
I don't have a mac handy so I can't verify if this would work.
我手边没有 mac,所以我无法验证这是否可行。
Try running su -
尝试运行 su -
Then running your script. If that works, try crontab -e
然后运行你的脚本。如果可行,请尝试 crontab -e
and adding an entry to run that script of yours.
并添加一个条目来运行您的脚本。
Are you familiar with crontab? well if not google it if need be. But basically to run it every day at midnight you'd have something like 0 * * * * /path/to/script
你熟悉 crontab 吗?好吧,如果不是谷歌的话,如果需要的话。但基本上每天在午夜运行它你会得到类似 0 * * * * /path/to/script 的东西