Conditional judging Type
As long as the procedure, then the conditional judgment, that is, if then this discriminant must be learned, the other is Case...esac
If....then
This is If...then is the most common conditional judgment, it is divided into single-branch conditional judgment, double-branch conditional judgment, multi-branch conditional judgment type
Single-branch conditional judgment statement:
If [conditional judgment]
Then
Output content
Fi
Two-branch conditional judgment:
If [conditional judgment]
Then
Output content
Else
Output content
Fi
Multi-branch conditional judgment
If [conditional judgment]
Then
Output content
elif [Conditional Judging type]
Then
Output content
Else
Output content
Fi
There are 3 common methods for the comparison of conditional judgments, namely, numerical comparison, character comparison, and file comparison.
Numerical comparison:
Integer comparison
-EQ: Tests whether two integers are equal, such as $a-eq $B
-ne: Test whether two integers are unequal: unequal, true: equal, False
-GT: Tests whether a number is greater than another number: greater Than, true: otherwise, false
-LT: Tests whether one number is less than the other: less Than, true: otherwise, false
-ge: greater than or equal to
-le: Less than or equal to
Character comparison:
STRING1 = STRING2: Indicates equal characters
STRING1! = STRING2: Indicates unequal characters
File comparison:
-E: Indicates whether the file exists
-F: Indicates that the file exists and is a normal file
# #还有很多选项, the above two are more commonly used
Exercise: Write a script
Determine if there is a user default shell for bash on the current system:
If so, how many of these users are displayed: otherwise, no such user is shown
[Email protected]_2 ~]# Cat ba.sh
#!/bin/bash
Bashuser= ' grep ' \<bash\> "/etc/passwd |cut-d": "-F 1 '
If [$?-eq 0];then
echo "The following users have bash:"
echo "${bashuser}"
Else
echo "No user has bash"
Fi
Exercise: Write a script
Given a file, such as/etc/inittab
Determine if there is a blank line in this file
If available, displays the number of blank lines: Otherwise, no blank lines are displayed
[Email protected]_2 ~]# Cat hang.sh
#!/bin/bash
hang= ' grep ' ^$ '/etc/inittab |wc-l '
If [! $?-eq 0];then
Exit 8
Fi
If [$hang-gt 0];then
echo "has ${hang} row blank line"
Else
echo "No blank line"
Fi
Exercise: Write a script
Given a user, determine if the UID is the same as the GID
If it does, the user is displayed as "good guy": Otherwise, this user will be shown as "bad guy"
[Email protected]_2 ~]# Cat ug.sh
#!/bin/bash
Useruid= ' id-u ' [email protected] ' 2>/dev/null '
If [! $?-eq 0];then
echo "Please pass in the correct user"
Exit 9
Fi
Usergid= ' id-g ' [email protected] ' 2>/dev/null '
If [! $?-eq 0];then
echo "Please pass in the correct user"
Exit 9
Fi
If [$#-eq 1];then
If [$useruid-eq $usergid];then
echo "Good guy"
echo "${1} UID and GID"
Else
echo "Bad guy"
Fi
Else
echo "Please pass in the correct parameters"
Fi
Exercise: Write a script
Determines whether the total number of history commands in the command history is greater than 1000, if greater than, displays "Some command will Gone": Otherwise "OK" is displayed.
[Email protected]_2 ~]# Cat his.sh
#!/bin/bash
#
his= ' History |wc-l 2>/dev/null '
If [$his-gt];then
echo "Some command would gone"
Else
echo "OK"
Fi
Case....esac judgment
Grammar
Case variable name in
Value one)
Output information
;;
Value two)
Output information
;;
*)
Output information
;;
Esac
#一般常用于传参
This article is from "think one or two" blog, please be sure to keep this source http://250919938.blog.51cto.com/962010/1918592
Shell condition judgment