简体   繁体   English

macOS - 以 root 用户身份运行 shell 脚本时切换到管理员用户

[英]macOS - Switch to admin user when running a shell script as a root user

So, I have this name.sh script with bunch of shell commands.所以,我有一个带有一堆 shell 命令的 name.sh 脚本。 I need to change the wallpaper before ending that script.我需要在结束该脚本之前更改壁纸。

I'm using我正在使用

osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/my.jpg"'

at the end of the line.在行尾。 The thing is the Apple Script won't run under root.问题是Apple Script 不会在root 下运行。 Because System Events will throw 10810 at first and then 600 (Application isn't running).因为系统事件首先会抛出 10810,然后是 600(应用程序未运行)。 If I run this osascript under admin user, it will work just fine.如果我在管理员用户下运行这个 osascript,它会工作得很好。 The wallpaper will be set.壁纸将被设置。

Let me know how you guys can help me in this!让我知道你们如何帮助我!

If you are already root , you have full privileges to switch to a different user account at any time.如果您已经是root ,则您拥有随时切换到其他用户帐户的完全权限。 The command for that is su .该命令是su

If your user is admin ,如果您的用户是admin

su - admin <<\:
    osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/dneg.jpg"'
:

There are various ways to pass the command as standard input to su ;有多种方法可以将命令作为标准输入传递给su perhaps see also Pass commands as input to another command (su, ssh, sh, etc)也可以参见Pass commands as input to another command (su, ssh, sh, etc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM