在 OS X 上以管理员权限运行脚本 [英] Running script with admin permissions on OS X

查看:27
本文介绍了在 OS X 上以管理员权限运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽力在 Stack Overflow 和互联网上找到了许多脚本问题的解决方案,但我似乎无法找到我需要的解决方案.

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.

我遇到了这段代码:

for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    dscl . -delete /Users/$cuser
done

如果我在终端中运行它,我会收到权限错误.所以我想我需要用 sudo 运行它.所以我开始研究创建 AppleScripts 来运行脚本,但我似乎找不到正确的方法.

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.

有什么想法吗?顺便说一下,我是在 Mac 上编写脚本的新手,所以请评论你的代码,这样我就知道发生了什么,所以我不会在不知道它会做什么的情况下运行一些脚本代码.:)

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. :)

谢谢

推荐答案

要使用 sudo 或管理员权限执行 shell 脚本,请将 with administrator privileges 附加到您的末尾do shell script 行.例如:

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:

do shell script "/path/to/script/file.sh" user name "adminusershortname" password "password" with administrator privileges

您可以在 Apple 的关于 do shell 的技术说明中找到更多信息脚本

You can find more on Apple's technote dealing with do shell script

也就是说,将其保存为 shell 脚本并使用 sudo 运行 shell 脚本也能正常工作.

That said, saving this as a shell script and running the shell script using sudo would work just as well.

#! /bin/sh

for cuser in `/usr/bin/dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    /usr/bin/dscl . -delete /Users/$cuser
done

另存为说removeUser.sh,使用chmod将其设置为可执行文件(chmod 755),然后运行它(sudo ./removeUser.sh)

Save it as say removeUser.sh, use chmod to set it as executable (chmod 755) and then run it (sudo ./removeUser.sh)

这篇关于在 OS X 上以管理员权限运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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