Typically, as a deployment script for an application, the first task that starts is to create a dedicated (dedicated) user and user group for the current app. This script is very easy. Here is a sample of references:
#!/bin/shuser=test_usergroup=test_group#create group if not existsegrep "^ $group"/etc/group >&/dev/nullif [$?- NE 0]then groupadd $groupfi #create user if not existsegrep "^ $user"/etc/passwd >&/dev/nullif [$?-ne 0]the n useradd-g $group $userfi
For the user to join, we can also use the ID command to infer whether a user exists, so that the creation of a user's script can write:
#create user if not existsid $user >&/dev/nullif [$?-ne 0]then useradd-g $group $userfi
But. Using the ID command does not infer if a user group already exists! As for the use of the id-g $user can only give an existing user group to which they belong, and cannot infer whether a user group already exists. Therefore, in order to use the script to handle the unified. We use the same method of finding from/etc/group and/etc/passwd files to infer whether a user group or user exists!
Shell script: Infer whether users and user groups already exist/create users and user groups