2009-08-24 64 views
3

我目前正在为内部程序开发Debian软件包。作为这个软件包的一部分,我需要创建程序的大部分功能运行的用户。我正在postinst脚本中执行此操作。 postinst脚本可以运行多次(例如在升级时),所以确保我不会每次尝试创建用户都很重要。确保在Debian GNU/Linux系统上存在用户

那么,如何确保仅在脚本运行的第一次时创建用户,而不会影响脚本的后续运行?

回答

2

你不需要知道用户是否存在。如果用户已经存在相同的参数,adduser(8)不会返回错误。从手册页:

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. 
1

如mentionned才可以使用的“ID”命令,如果你想获得系统中的所有用户可以使用:

getent passwd 

这将列出所有系统上的用户(即使他们在远程数据库上,如ldap或nis等)。

+0

好的答案。可能比id更强大。 – 2009-08-30 12:47:39