1

I'm writing a bash script that creates directories and copy files under Mac OSX. Some of these directories and files need to be placed in folders owned by the system such as /Library/Audio/Plug-Ins, and so I run the script under sudo. Such script might look like:

copy-plugins.sh:

#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins

and called:

$ sudo ./copy-plugins.sh

However when running under sudo, all created directories and copied files are owned by root.

I would like to be able to run the script under sudo and have the files be owned by my user. I could call chown after each file/directory is created or copied

copy-plugins-cumbersome.sh:

#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins

cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins/plugin-A.dylib

cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501  /Library/Audio/Plug-Ins/My-Plugins/plugin-B.dylib

but I'm hoping for a more general solution.

As far as I can tell there is no setuid for bash.

Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32

2 Answers2

2

Use cp -p option to preserve file attributes.

Note this will preserve user, group permissions and the modification and access times of the files.

suspectus
  • 16,548
  • 8
  • 49
  • 57
  • Using cp -p creates the files with _unknown user and group: ` -rwxr-xr-x 1 _unknown _unknown 0 28 Apr 12:43 plugin-A.dylib` ` -rwxr-xr-x 1 _unknown _unknown 0 28 Apr 12:43 plugin-B.dylib` strange.... – Periodic Maintenance Apr 28 '14 at 11:41
  • It appears there is no way of setting uid from within a bash script, so I accept this answer although it only partially answers the question. Thanks! – Periodic Maintenance May 01 '14 at 21:43
  • hi - in case it helps you can set uid from sudo : `sudo -u 501 your-script.sh` – suspectus May 01 '14 at 22:45
1

As you need sudo to copy to the directories you are copying to in script, it means you need to be root to copy anything in those directories.

When you do sudo you are root for that particular command or script, so whatever will be created or executed will have root permissions. Till the time you specify.

The possible ways to come out of it without changing anything:

  1. The one you are using, and
  2. Other one to use -p or -a with cp
  3. rsync -go <source file> <destination file>

    -g for preserving group and -o for preserving ownership.

Note If you do a chown out of script, you will have to specifically do sudo chown since files you would be touching belong to root.

PradyJord
  • 2,136
  • 12
  • 19
  • Doing chown on every command requires that I know in advance the uid of the user. This prevents me from creating a generl script that can be used by all users. – Periodic Maintenance Apr 28 '14 at 11:45
  • no for doing chown user is not required to be known, you can use `id -u` for user id and `id -g` for groupid. Moreover I have added rsync as sol, check if it works for you or not. All of them are working for me. – PradyJord Apr 28 '14 at 11:49
  • Using rsync -go is better than chown as it does not require user specific customizations. However even with rsync copied files get _unknown user and group. – Periodic Maintenance Apr 28 '14 at 11:49