Shell script: Determine whether a user or user group already exists/create a user or user group

Source: Internet
Author: User

Shell script: Determine whether a user or user group already exists/create a user or user group

Generally, as a deployment script for an application, the first task is to create a dedicated user and user group for the current application. This script is very simple. Here is a reference sample:


#! /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] then useradd-g $ group $ userfi

For adding users, we can also use the id command to determine whether a user exists, so that a user's script can be written as follows:

# Create user if not existsid $ user> &/dev/nullif [$? -Ne 0] then useradd-g $ group $ userfi

However, you cannot use the id command to determine whether a user group already exists! To use id-g $ user, you can only give the user group to which an existing user belongs and cannot determine whether a user group exists, we use the/etc/group and/etc/passwd files to check whether a user group and user exists!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.