简体   繁体   中英

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. 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. Because System Events will throw 10810 at first and then 600 (Application isn't running). If I run this osascript under admin user, it will work just fine. 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. The command for that is su .

If your user is 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 ; perhaps see also Pass commands as input to another command (su, ssh, sh, etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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