I'm obviously very new to this. I'm trying to write a simple for loop that will read the directory names in /Users and then copy a file into the same subdir in each user directory.
I have this, and it works but it isn't great.
#!/bin/bash
HOMEDIRS=/Users/*
for dirs in $HOMEDIRS; do
if [[ -d "$dirs" && ! -L "$dirs" ]]; then
#copy the config file from source to destination
/bin/cp "/Applications/tn3270 X.app/tn3270 preferences" $dirs/Library/Preferences/;
fi;
done
I'd like to be able to exclude some directory names ("Shared" and "shared_space"). Also, it seems like it might be awkward to construct the copy line as I did.
I'd appreciate any advice on how to do this better. Thanks!
In case $HOMEDIRS are really user home directories,
I guess that the files that you create in /Users/*/Library/Preferences/ have the wrong owner?
Then a better method is a start wrapper e.g. named "tn3270" in the user's path
$HOMEDIRS are user directories. They also match the user name for each user, which is useful.
You are right that ownership will be a problem. Unfortunately the script will be run by a patch management tool (Munki) so the current user will not be the owner of any of the home directories.
If I can set the owner of the file to the name of the home directory that should work. I can nest that in the same loop that copies the file each time.
I had thought that the source file should be bundled with the application (tn3270 x) but maybe I should bundle it with the script and put it in /tmp and then read it from /tmp each time.
This doesn't work, but something more like this maybe?
#!/bin/bash
HOMEDIRS=/Users/*
#$HOMEDIRS here needs to be just the directory name, not the absolute path
for dirs in $HOMEDIRS; do
if [[ -d "$dirs" && ! -L "$dirs" ]]; then
#copy the config file from source to destination
/bin/cp "/tmp/tn3270 preferences" $dirs/Library/Preferences/;
#set ownership on the preferences file
/bin/sbin/chown $HOMEDIRS $dirs/Library/Preferences/tn3270\ preferences;
fi;
done
Any advice? If I'm not being clear on what needs to happen please tell me. I can try to explain it better. Thanks!
Thanks for all of the suggestions! The file could potentially be modified by each user over time, so a discreet copy for each user is the best way to do it I think.
The suggestion by bacarrdy is working great. This helps me understand how these loops work much better. Like I said, I am a total beginner with scripting. Thanks again.