Basic help improving for in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic help improving for in loop
# 1  
Old 06-02-2014
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.

Code:
#!/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!
# 2  
Old 06-02-2014
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
Code:
#!/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.
# 3  
Old 06-03-2014
$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?
Code:
#!/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!
# 4  
Old 06-03-2014
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?
# 5  
Old 06-03-2014
you want to do somethink like this? i dont understand what you cant to do
Code:
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


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 06-03-2014 at 11:05 AM..
This User Gave Thanks to bacarrdy For This Post:
# 6  
Old 06-03-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic FOR loop with break

Oracle Linux : 6.4/bash shell In the below I want to break out of the loop when it enters the 5th iteration. #!/bin/bash for i in 1 2 3 4 5 6 do echo "$i" if echo "Oh Nooo... i = $i. I need to stop the iteration and jump out of the loop" then break fi done But, it only... (3 Replies)
Discussion started by: John K
3 Replies

2. UNIX for Dummies Questions & Answers

Basic loop awk/shell script question..

Hi, Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this. The basic objective is to copy certain files that are scattered all over my... (10 Replies)
Discussion started by: pc2001
10 Replies

3. Shell Programming and Scripting

Trying to run a basic for loop

OS : RHEL 6.1 Shell : Bash I had a similair post on this a few weeks back. But I didn't explain my requirements clearly then. Hence starting a new thread now. I have lots of files in /tmp/stage directory as show below. I want to loop through each files to run a command on each file. I... (8 Replies)
Discussion started by: kraljic
8 Replies

4. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

5. Shell Programming and Scripting

Unix Shell basic loop massive n00b

hey guys I would really appreciate some help, i need to do a project for a job that requires minimal UNIX scripting and im REALLY stuck basically Im stuck at what i believe is something really simple but i just dont have a clue how to do it efficiently and properly and i REALLY appreciate some... (16 Replies)
Discussion started by: thurft
16 Replies

6. Shell Programming and Scripting

Basic While loop won't exit...

Hi everyone - just like to say great forum...I've learned a lot off here but I just can't figure this one out...(first post) I'm writing a script to monitor a directory and email the latest modified file....(I realize there are better ways than I'm trying here...I don't like copying and pasting... (5 Replies)
Discussion started by: trevthefatty
5 Replies

7. Shell Programming and Scripting

really basic for loop question

sorry for being dumb here, but is there a way my for loop can take an entire line of a file into consideration instead of each word in a line... ill explain if i have a file like this # cat list serial: 23124 hostname: server1 and a script that does this # cat list.sh #!/bin/sh ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Shell Programming and Scripting

Basic bash 'for loop' usage

Hi! I have a simple question about using a for loop. I'm trying to open up all the zip files in the currect directory with ark, but I am getting the error "bash: syntax error near unexpected token `for $i ; do ark $i ; done ; I looked in the info pages for bash, but I can't seem to figure... (2 Replies)
Discussion started by: Orange Stripes
2 Replies

9. UNIX for Dummies Questions & Answers

A basic question of FOR loop

Hi, have a basic query. Please see the below code: list="one two three" for var in $list ; do echo $var list="nolist" Done Wht if I want to print only first/ last line in the list Eg one & three Regards er_ashu (3 Replies)
Discussion started by: er_ashu
3 Replies
Login or Register to Ask a Question