Skip to main content

As a rookie question, I wondered how you'd either make a directory or move contents of old folder to another - as a user.   I know policies run as root but if I want it to do something in the users directory - how would I execute that?

Getting the user, I can do: curUser=$( stat -f%Su /dev/console )

If I want to run something like "mkdir foo" shouldn't it be something like:

su - "$curUser" -c 'mkdir "/Users/$curUser/foo"'

Although I try that as a policy for my own computer and it doesn't do anything.

The goal being to put the contents of one directory in another so their old data isn't overwritten 

 

 

@ChuckFinley For everything you want to know about running a command as another user on macOS see https://scriptingosx.com/2020/08/running-a-command-as-another-user/


Hi @ChuckFinley ,

You do not need to use su. Instead you can run the command as mkdir "/Users/$curUser/foo"' and then ditto the content and use chown and chmod to set the permission.

 

 

 

#!/bin/bash loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' ) mkdir /Users/$loggedInUser/foo ditto <sourcepath> /Users/$loggedInUser/foo/ chown -R root:staff /Users/$loggedInUser/foo chmod -r 700 /Users/$loggedInUser/foo/ exit 0

 

 

Thanks


like this:

#!/bin/sh # template script for running a command as user # The presumption is that this script will be executed as root from a launch daemon # or from some management agent. To execute a single command as the current user # you can use the `runAsUser` function below. # by Armin Briegel - Scripting OS X # # sample code for this blog post # https://scriptingosx.com/2020/08/running-a-command-as-another-user/ # Permission is granted to use this code in any way you want. # Credit would be nice, but not obligatory. # Provided "as is", without warranty of any kind, express or implied. # variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # global check if there is a user logged in if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then echo "no user logged in, cannot proceed" exit 1 fi # now we know a user is logged in # get the current user's UID uid=$(id -u "$currentUser") # convenience function to run a command as the current user # usage: # runAsUser command arguments... runAsUser() { if [ "$currentUser" != "loginwindow" ]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # uncomment the exit command # to make the function exit with an error when no user is logged in # exit 1 fi } # main code starts here runAsUser mv /path/to/directory/to/move /path/to/new/location exit 0

hope that helps.

M


Hi @ChuckFinley ,

You do not need to use su. Instead you can run the command as mkdir "/Users/$curUser/foo"' and then ditto the content and use chown and chmod to set the permission.

 

 

 

#!/bin/bash loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' ) mkdir /Users/$loggedInUser/foo ditto <sourcepath> /Users/$loggedInUser/foo/ chown -R root:staff /Users/$loggedInUser/foo chmod -r 700 /Users/$loggedInUser/foo/ exit 0

 

 

Thanks


THANKS!  I got the mkdir to work!

One more if I may?  Can you do a mv with a variable?

Like if I set

mypath="/Users/$loggedInUser/foo

mv $mypath /Users/Shared/foo2

That doesn't work, It just gives me:

usage: mv [-f | -i | -n] [-hv] source target

I tried variations with "$mypath" and "${mypath}" with no change

       mv [-f | -i | -n] [-v] source ... directory


like this:

#!/bin/sh # template script for running a command as user # The presumption is that this script will be executed as root from a launch daemon # or from some management agent. To execute a single command as the current user # you can use the `runAsUser` function below. # by Armin Briegel - Scripting OS X # # sample code for this blog post # https://scriptingosx.com/2020/08/running-a-command-as-another-user/ # Permission is granted to use this code in any way you want. # Credit would be nice, but not obligatory. # Provided "as is", without warranty of any kind, express or implied. # variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # global check if there is a user logged in if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then echo "no user logged in, cannot proceed" exit 1 fi # now we know a user is logged in # get the current user's UID uid=$(id -u "$currentUser") # convenience function to run a command as the current user # usage: # runAsUser command arguments... runAsUser() { if [ "$currentUser" != "loginwindow" ]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # uncomment the exit command # to make the function exit with an error when no user is logged in # exit 1 fi } # main code starts here runAsUser mv /path/to/directory/to/move /path/to/new/location exit 0

hope that helps.

M


Thanks.  I wanted to work out one more step with the mv as noted above.

I want to be able to define a variable and then use mv $variable /path/to/dest


THANKS!  I got the mkdir to work!

One more if I may?  Can you do a mv with a variable?

Like if I set

mypath="/Users/$loggedInUser/foo

mv $mypath /Users/Shared/foo2

That doesn't work, It just gives me:

usage: mv [-f | -i | -n] [-hv] source target

I tried variations with "$mypath" and "${mypath}" with no change

       mv [-f | -i | -n] [-v] source ... directory


Hi @ChuckFinley , If you want to merge the content and do no want to replace you can use ditto.

#!/bin/bash loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' ) mkdir -p /Users/$loggedInUser/destination ditto /Users/$loggedInUser/source /Users/$loggedInUser/destination chown -R $loggedInUser:staff /Users/$loggedInUser/destination chmod -R 700 /Users/$loggedInUser/destination exit 0

mv command should work with variable as well. For ex:

#!/bin/bash loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' ) mypath="/Users/$loggedInUser/foo" mv $mypath /Users/Shared/foo2/

 

Thanks