Grimlockz
Grimlockz

Reputation: 2581

Loop through list of names and copy file explained?

I've written a script to copy a new user account file to the new user location. It works by reading a list of usernames and copying the file to that location. I can't understand why I needed the done < $USER at the end. Can someone please explain this?

Thanks

USER=/home/example/new.txt
NEWUSER=$USER

LOC=/var/account/          

cd /home/example

while read NEWUSER
do
cp _newuser.txt $LOC/$NEWUSER
done < $USER

Upvotes: 1

Views: 177

Answers (2)

Bill
Bill

Reputation: 5772

To iterate over each line in the file /home/example/new.txt, which is the value of variable USER

Pls look at http://en.kioskea.net/faq/1757-how-to-read-a-file-line-by-line

< is input redirection operator (http://www.tldp.org/LDP/abs/html/io-redirection.html)

You can also delete NEWUSER=$USER, since I do not see any use of NEWUSER except the while loop. Due to the while, NEWUSER will be assigned a new value each iteration.

Upvotes: 2

maverik
maverik

Reputation: 5586

Because read reads the input from standard input (stdin). In order to read from file you need to redirect it to the read command.

Upvotes: 0

Related Questions