File permission issue with script.

jason_wilsey
New Contributor

When running this script, the currentUser does not get proper permissions if the file did not exist prior to running. The root account is assigned permissions. How do I correct this? I would like to use chown command.

!/usr/bin/env sh

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

if [[ ! -d /Users/"$currentUser"/.FOLDER/ ]]; then mkdir /Users/"$currentUser"/FOLDER
fi

if [[ ! -f /Users/"$currentUser"/.ssh/config ]]; then touch /Users/"$currentUser"/FOLDER/FILE
else echo "User has a pre-existing file"
fi

cat << EOF >> /Users/"$currentUser"/FOLDER/FILE
Blah blah blah
EOF

3 REPLIES 3

howie_isaacks
Valued Contributor II

Any time I have used a script to create folders within the users' home directory, I have always followed up by adding a chown command to change the ownership of the folder(s) to the current user. Here's one I put together a few years ago that has worked consistently.

!/bin/sh

currentuser=/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'
echo $currentuser

mkdir $HOME/Documents/PDFs for Client Reports
mkdir $HOME/Documents/PDFs for ATBs;
mkdir $HOME/Documents/PDFs for Estimates;
mkdir $HOME/Documents/PDFs for Insertion Orders

chown $currentuser $HOME/Documents/PDFs for Client Reports
chown $currentuser $HOME/Documents/PDFs for ATBs
chown $currentuser $HOME/Documents/PDFs for Estimates
chown $currentuser $HOME/Documents/PDFs for Insertion Orders

jason_wilsey
New Contributor

The script will first check if a directory is there if not make it, then if the file exists append it if not make the file and input information. The issue is that if the folder or file does not exist they get made with root. I need to give privileges back to the user.
I need to modify this script to chown the directory and the file. Adding the following does not appear to work for the script.

sudo chown /Users/"$currentUser" /Users/"$currentUser"/FOLDER/FILE

howie_isaacks
Valued Contributor II

You need to add the command "echo $currentuser" or echo "$currentUser" as you set at the beginning of your script. What I posted above works every time it's ran either automatically after enrollment, or when someone runs the policy through Self Service. Without the echo command, the rest of the script doesn't work, since it has no idea who "currentUser" is.