{ 9 comments… read them below or add one }

1 Casper Pedersen January 3, 2008

This will also copy files to home directory of system users (not all of them have home directories).

This will prevent that this happens.

ID=$(id -u $u)
if [ $ID -ge 1000 ] ; then
if [ -d ${UHOME}/${u} ] ; then
… do copy here….
fi
fi

Reply

2 vivek January 3, 2008

Casper,

Yeah, you got a valid point there.

Appreciate your post.

Reply

3 Ilias Marinos January 3, 2008

marinosi@lucifer:~$ id -u nobody
65534

I’m pretty sure you don’t want to copy something to the nonexistent nobody’s homedir. :-P

Someone , could have in the system other accounts that cannot login (or don’t have a home dir etc)..One way to find the active users of a system is :

$ cat /etc/shadow | grep -v ‘[*!]‘

Users who are inactive(have disabled accounts ) have a * or $ (these users cannot login) at the field where they should have the password hash.

Reply

4 vivek January 3, 2008

Ilias,

The post has been updated. Thanks for sharp observation.

Reply

5 Jeff Schroeder January 3, 2008

Instead of using cp… chown… use the install command. It does the same thing.

Reply

6 Vincent January 7, 2008

If you don’t have your users in /etc/passwd, but for instance in ldap, you can use `getent passwd` instead.

Reply

7 Alex Gretlein May 3, 2008

You also need to safely handle pre-existing files of the same name. You can use the –backup option with install or cp, but you probably want to log it and/or inform users in some way.

Reply

8 Indie September 11, 2009

The users home directory isn’t necessarily in the /home directory, you should read it from the ‘directory’ field in /etc/passwd – field 6
UHOME=$(grep '^${u}' /etc/passwd | cut -d: -f6)

Reply

9 gerar April 8, 2011

hmm… errr…..

if you chown $_dir/${FILE} then you are trying to chown a nonexistant file,
as this expands to /home/user//nas05/.newconfig-file

chown $(id -un $u):$(id -gn $u) “$_dir/${FILE}”

Maybe if you created a new variable:
FILENAME=”.newconfig-file”

then you changed the chown line:
chown $(id -un $u):$(id -gn $u) “$_dir/${FILENAME}”
it would expand properly?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 5 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: