Linux conditional judgment: Common practice to add users

Source: Internet
Author: User
Tags stdin

1, practice writing a script to complete the following requirements:

1, add 3 users User1,user2,user3, but first to determine whether the user exists, does not exist and then add;

2, added after the completion, the display has added a total of several users, of course, can not be included because of pre-existing and not added;

3, finally shows how many users on the current system;

#!/bin/bash

#program

# Practice writing to add user command scripts

# History

# time:2016-10-19-9:00

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

! ID user1 &>/dev/null && useradd user1 &>/dev/null && echo "user1" | passwd--stdin user1 &>/dev/null | | echo "User1 is exist."

! ID user2 &>/dev/null && useradd user2 &>/dev/null && echo "User2" | passwd--stdin user2 &>/dev/null | | echo "user2is exist."

! ID user3 &>/dev/null && useradd user3 &>/dev/null && echo "User3" | passwd--stdin user3 &>/dev/null | | echo "User3 is exist."

usernum= ' Wc-l/etc/passwd | Cut-d '-f1 '

echo "Users is $USERNUM."


2, practice writing a script to complete the following requirements:

given a user:

1, if its UID is 0, this is displayed as an administrator;

2, otherwise, it is displayed as a normal user;

#!/bin/bash

2 #program

3 # Practice Determining if a user is root

4 #history

5 #time 2016-10-19-9:38

6 Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

7 Export PATH

8 Username=user1

9 userid= ' Id-u $USERNAME '

[$USERID-eq 0] && echo "$USERNAME is Admin" | | echo "$USERNAME is command"


3, practice writing a script to complete the following tasks:

1, use a variable to save a user name;

2, delete the user in this variable, and delete their home directory;

3, display the "User Delete complete" class information;

#!/bin/bash

#program

# Practice to determine if a user exists and delete this user and home directory if it exists.

#history

#time 2016-10-19-9:53

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Username=user1

ID $USERNAME &>/dev/null && userdel-r $USERNAME && echo "$USERNAME is del" | | echo "$USERNAME is not exist."


This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1863268

Linux conditional judgment: Common practice to add users

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.