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