Typically, as a deployment script for an application, the first task that begins is to create a dedicated (dedicated) user and user group for the current app. This script is very simple, a reference sample is attached here:
[Plain]View Plaincopy
- #!/bin/sh
- User=test_user
- Group=test_group
- #create group if not exists
- Egrep "^ $group"/etc/group >&/dev/null
- If [$?-ne 0]
- Then
- Groupadd $group
- Fi
- #create user if not exists
- Egrep "^ $user"/etc/passwd >&/dev/null
- If [$?-ne 0]
- Then
- Useradd-g $group $user
- Fi
For adding users, we can also use the ID command to determine whether a user exists, so that the creation of a user's script can be written like this:
[Plain]View Plaincopy
- #create user if not exists
- ID $user >&/dev/null
- If [$?-ne 0]
- Then
- Useradd-g $group $user
- Fi
However, using the ID command does not tell if a user group already exists! As for the use of id-g $user can only give an existing user to belong to the user group is what, and can not tell whether a user group is already exist, so, in order to use the script to handle the unified, we use unified from/etc/group and/etc/ passwd file to find out the way to determine whether a user group and users exist!
Shell script: Determine whether users and user groups already exist/create users and user groups