It is currently Fri May 25, 2018 9:59 pm

Need help in laying out a do while loop.

All times are UTC - 6 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Jul 17, 2012 11:04 am   

Joined: Tue Jul 17, 2012 10:59 am
Posts: 1
So I'm creating a script at work that will automatically change preferences of the screensaver preference file, then reapply chmod and chown to that file. I want it to run for all users on that computer minus the standard accounts like Guest or Shared.
This is what I've got so far.


echo copying file
sudo cp -v PolicyBanner.rtf /Library/Security
echo changing permission
sudo chmod -R o+rw /Library/Security/PolicyBanner.rtf

#What I'm doing here is listing all the user directories in /Users, minus "Shared" - I guess I should include "Guest" as well. The point is to modify a specific file in each user directory (/Users/<user>/Library/Preferences/<file>.
Then reapply own ownership and permission to the file so it will take effect.
Otherwise OSX will throw out the changes thinking that it is corrupt.

# I want to take the output ($i) and put it into an array to read so when I run
the defaults, chown, and chmod commands, it's running them for the users own file.

cd /Users/

#The for loop gets all the users minus Shared. Then I want to add those into an array so when I run the chmod, chown command they modify that users specific file.

for i in `ls`;


if [ $i == "Shared" ]; then



echo $i


sudo defaults write </Users/<user>/Library/Preferences/<file>

sudo chown <user>  </Users/<user>/Library/Preferences/<file>

sudo chmod 600 </Users/<user>/Library/Preferences/<file>

echo securing system preferences

sudo chmod o-r /System/Library/PreferencePanes/<file>

echo done!

 PostPosted: Tue Jul 17, 2012 1:27 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 643

- if you have to sudo things inside a scripts, then sudo the whole script
- you'd better not use `ls` inside scripts; instead use shell globbings (see man bash /^ *Pathname Expansion), the test should then become useless.
- generally, always quote variables, especially inside tests, and when passed to a command.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group