在 OS X 上以管理员权限运行脚本
我已经尽力在 Stack Overflow 和互联网上找到许多脚本问题的解决方案,但我似乎找不到我需要的解决方案。
我想要做的是创建一个更加自动化且点击次数更少的解决方案来删除系统上的所有移动缓存用户帐户。我一直在登录并手动转到用户帐户,然后通过单击“-”按钮一次删除一个用户,然后单击用户数据的“立即删除”。这可行,但很耗时,而且我有更重要的事情要做。所以我知道必须有一种方法可以通过脚本来做到这一点。
我遇到了这段代码:
for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
dscl . -delete /Users/$cuser
done
如果我在终端中运行它,我会收到权限错误。所以我想我需要用 sudo 运行它。所以我开始考虑创建 AppleScripts 来运行脚本,但我似乎找不到正确的方法。
有什么想法吗?顺便说一句,我是 Mac 上的脚本新手,所以请评论您的代码,以便我知道发生了什么,这样我就不会只是运行一些脚本代码而不知道它会做什么。 :)
谢谢
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.
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.
I ran across this code:
for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | 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.
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. :)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使用
sudo
或管理员权限执行 shell 脚本,请将具有管理员权限
附加到do shell script
行的末尾。例如:您可以在 Apple 的技术说明中找到更多有关
do shell 的信息script
也就是说,将其保存为 shell 脚本并使用 sudo 运行 shell 脚本也可以。
将其保存为removeUser.sh,使用
chmod
将其设置为可执行文件(chmod 755
),然后运行它(sudo ./removeUser.sh)
To perform a shell script with
sudo
or administrator privileges appendwith administrator privileges
to the end of yourdo shell script
line. For example:You can find more on Apple's technote dealing with
do shell script
That said, saving this as a shell script and running the shell script using sudo would work just as well.
Save it as say removeUser.sh, use
chmod
to set it as executable (chmod 755
) and then run it (sudo ./removeUser.sh
)您可以通过编辑系统的 sudoers 文件来完成此操作。这将允许您用来运行此脚本的帐户(通过 cron 等)无需密码即可运行 sudo。
要编辑 sudoers 文件,您可以使用 visudo,但它必须以管理员权限运行。尝试:
在文件末尾添加如下行,将 user_name 替换为将运行脚本的用户。请注意,每个字段之间使用制表符。
现在 user_name 应该能够输入 sudo 并且不会提示输入密码。
另请注意,visudo 是一个文本编辑器,它镜像 vi 编辑器并使用与 vi 相同的命令。
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.
To edit the sudoers file you use visudo, but it must be run with admin permission. Try:
Add a line like the following to the end of the file, replacing user_name with the user who will run your script. Note, use tabs between each field.
Now user_name should be able to type sudo and will not be prompted for a password.
Also note that visudo is a text editor that mirrors the vi editor and uses the same commands as vi.
我手边没有 Mac,所以无法验证这是否有效。
尝试跑步
su -
然后运行你的脚本。如果有效,请尝试
crontab -e
并添加一个条目来运行您的脚本。
你熟悉crontab吗?好吧,如果需要的话,如果不谷歌它。
但基本上每天午夜运行它你会得到类似的东西
0 * * * * /path/to/script
请参阅:http://en.wikipedia.org/wiki /计划任务
I don't have a mac handy so I can't verify if this would work.
Try running
su -
Then running your script. If that works, try
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
See: http://en.wikipedia.org/wiki/Cron