在 OS X 上以管理员权限运行脚本

发布于 2024-08-04 06:34:05 字数 604 浏览 8 评论 0原文

我已经尽力在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

维持三分热 2024-08-11 06:34:06

要使用 sudo 或管理员权限执行 shell 脚本,请将具有管理员权限 附加到 do shell script 行的末尾。例如:

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

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

也就是说,将其保存为 shell 脚本并使用 sudo 运行 shell 脚本也可以。

#! /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)

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

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.

#! /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

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

有木有妳兜一样 2024-08-11 06:34:06

您可以通过编辑系统的 sudoers 文件来完成此操作。这将允许您用来运行此脚本的帐户(通过 cron 等)无需密码即可运行 sudo。

要编辑 sudoers 文件,您可以使用 visudo,但它必须以管理员权限运行。尝试:

$ sudo visudo

在文件末尾添加如下行,将 user_name 替换为将运行脚本的用户。请注意,每个字段之间使用制表符。

user_name    ALL=(ALL)     NOPASSWD:ALL

现在 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:

$ sudo visudo

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.

user_name    ALL=(ALL)     NOPASSWD:ALL

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.

卖梦商人 2024-08-11 06:34:06

我手边没有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文