views:

71

answers:

3

I'm currently working on a Debian package for an in-house program. As part of this package, I need to create the user which most of the functionality of the program runs as. I am doing this in the postinst script. The postinst script can be run several times (on upgrade, for example), so it's important to ensure that I'm not going to attempt to create the user every time.

So, how can I ensure that the user is created only the first time that the script is run, without affecting it on later runs of the script?

+8  A: 

Try:

[aiden@dev ~]$ id aiden
uid=500(aiden) gid=500(aiden) groups=500(aiden)
[aiden@dev ~]$ id foomonkey
id: foomonkey: No such user
[aiden@dev ~]$

The first $? is 0, the second is 1.

Aiden Bell
+1  A: 

You do not need to know whether the user exists or not. adduser(8) will not return an error if the user already exists with the same parameters. From the man page:

EXIT VALUES
       0      The  user  exists as specified. This can have 2 causes: The user
              was created by adduser or the user was already  present  on  the
              system  before  adduser  was  invoked. Invoking adduser a second
              time with the same parameters as before also returns 0.
camh
+1  A: 

as mentionned before you can use the 'id' command, if you like to get all the user in a system you can use :

getent passwd

which will list all the users on the system (even if they are on a remote database like ldap or nis and etc...)

Chmouel Boudjnah
Nice answer. Probably more robust than id.
Aiden Bell