Linux Judgment statement: if statement

Source: Internet
Author: User

Introduction to the IF command:

This command is used for conditional judgments, control structures.

1 . Command format:

Single Branch if statement

if judgment condition; then

statement1 statement 1

statement2 statement 1

......

Fi

Dual-Branch if statement

if judgment condition; then

Statement1

Statement2

......

Else

Statement3

Statement4

......

Fi

2 . Command function:

Make changes to the related operations of the group properties.

3 . Command parameters:


4 . Command instance:

1. If the user already exists, the display already exists, the hint exists, if it does not exist, the user is added, and

Set the password for this user name.

#!/bin/bash

# program:

# Practice adding users with an if judgment statement

# History

# time:2016-10-19-10:46

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

Export PATH

Name=user1

If ID $NAME &>/dev/null; Then

echo "$NAME is exist."

Else

Useradd $NAME

echo "$NAME" | passwd--stdin $NAME &>/dev/null

echo "$NAME is add sucessful"

Fi

2. If the user already exists, the display already exists, delete this user and delete the user's home directory, and prompt to delete the successful

If it does not exist, the user is prompted to do not exist.

#!/bin/bash

# program:

# Practice using the IF Judgment statement to delete a user

# History

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

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

Export PATH

Name=user1

if! ID $NAME &>/dev/null; Then

echo "The $NAME is not exist."

Else

Userdel-r $NAME &>/dev/null

echo "The $NAME is del successful."

Fi

3, given a user, if its uid is 0, this is displayed as an administrator, otherwise, it is displayed as a normal user;

#!/bin/bash

# program:

# Practice using the If judgment statement to determine whether the user is an administrator

# History

# time:2016-10-19-11:34

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

Export PATH

Name=user1

Nameid= ' Id-u $NAME '

If [$NAMEID-eq 0]; Then

echo "The $NAME is Admin."

Else

echo "The $NAME is common."

Fi

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

Linux Judgment statement: if statement

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.