Add a user using a shell script

Source: Internet
Author: User
Tags file permissions

This article demonstrates how to use a shell script to complete adding a user, first making a decision if the user exists, prompting the user to already exist, or adding a new user.

The sample code is as follows:

1#!/bin/Bash2 3 Grep_user ()4 {5R= 'grep "^$1:"/etc/passwd|WC-l '6     Echo$R7 }8 9 Echo-N"input Username:"Ten  One Read USERNAME A  -ret=' Grep_user $USERNAME ' - Echo ".... return $RET .... ." the if[$RET-eq1 ] -  Then -     Echo "$USERNAME exist" - Else +     Echo "$USERNAME not exist" -     Echo "plz add a new user" +     sudoAddUser $USERNAME A fi

I use the Ubuntu12.04 environment, the following steps:

1. Create a new file in the home directory.

2, change the file permissions, chmod 755 username.sh

3. Execute the script./username.sh

I want to add a user named Linux, before adding:

No Linux users.

After executing the script:

Follow the prompts to step through the steps and the end result is:

At the same time can be found under the home of Linux users, if the newly added user already exists, then the hint already exists, resulting in unable to add, the entire process is completed!

Add a user using a shell script

Related Article

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.