Running script with administrator privileges on OS X

I tried my best to find a solution with numerous script questions on stack overflow and internet, but I can not find the solution I need.

What I want to do is create a more automatic and less clickable solution to delete all mobile cached user accounts on the system. I log in and manually go to user accounts and delete users one at a time by clicking the โ€œ-โ€ button and then โ€œDelete immediatelyโ€ for user data. It works, but it 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 came 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 the terminal, I get permission errors. So I decided that I need to run it using sudo. So I started learning how to create AppleScripts to run a script, but I cannot find the right way to do this.

Any ideas? By the way, I am new to writing scripts on Mac, so please comment on your code so that I know what is happening, and therefore I do not just run the script code, I donโ€™t know what it will do. :)

thanks

+3
source share
3 answers

To execute a shell script with sudoor administrator privileges, add with administrator privilegesat the end of your line do shell script. For example:

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

You can find more about Apple technologies that concern do shell script

, script script sudo .

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

say removeUser.sh, chmod, (chmod 755), (sudo ./removeUser.sh)

+5

, sudoers . , script ( cron ..) sudo .

sudoers, visudo, . :

$ sudo visudo

, , _ , script. , .

user_name    ALL=(ALL)     NOPASSWD:ALL

_ sudo .

, visudo - , vi , vi.

+2

mac handy, , .

su -

script. , crontab -e

script .

crontab? , Google, . , , - 0 * * * */path/to/script

: http://en.wikipedia.org/wiki/Cron

0
source

Source: https://habr.com/ru/post/1716746/


All Articles