Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create a script to modify some data, which would require super user permission. Is there any way to automate this process via shell script on Mac 10?

Sample Script:

sed s/Hello/World/g /usr/local/opt/test.xml 

To run this script, the user needs super user permission, is there any way to provide root credentials in the script and execute?

share|improve this question

migrated from stackoverflow.com May 20 '11 at 23:30

This question came from our site for professional and enthusiast programmers.

    
does this need to be run on demand, or could it be run hourly say by cron? – PurplePilot May 19 '11 at 19:37

You can change the permissions on the shell script file to automatically make it run as root.

From the chmod manpage:

4000    (the set-user-ID-on-execution bit) Executable files with
        this bit set will run with effective uid set to the uid of
        the file owner.  Directories with the set-user-id bit set
        will force all files and sub-directories created in them to
        be owned by the directory owner and not by the uid of the
        creating process, if the underlying file system supports
        this feature: see chmod(2) and the suiddir option to
        mount(8).

A sample command might look like:

sudo chown root:root file.sh
sudo chmod 4755 file.sh
share|improve this answer
    
I am running this via shell script. I used expect to resolve this! thanks :) – Santosh M Jul 1 '11 at 9:44
    
I believe using the set-user-ID-on-execution bit won't work on a shell script. It must be a compiled program. – Gordolio Aug 10 '15 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .