Basic help improving for in loop

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

#!/bin/bash
cp "/Applications/tn3270 X.app/tn3270 preferences" $HOME/Library/Preferences/
exec /path/to/real/tn3270 "$@"

that will be run by the user, and files will be owned by the user.
/path/to/real/tn3270 should not be 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!

If the file(s) are same for all users, I think its better to keep it at some standard location and provide softlinks to all the users?

you want to do somethink like this? i dont understand what you cant to do

for dirs in $(ls /Users | grep -v Shared | grep -v shared_space) do

if [[ -d "/Users/$dirs" && ! -L "/Users/$dirs" ]]; then
#copy the config file from source to destination
/bin/cp "/Applications/tn3270 X.app/tn3270 preferences" /Users/$dirs/Library/Preferences/;
chown $dirs:$dirs /Users/$dirs/Library/Preferences/tn3270\ preferences;
fi;

done 
1 Like

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.