If statement formatting
If condition
Then
Command
Else
Command
Fi, don't forget this ending.
If statement forgets the end fi
Three conditional expressions of Test.sh:line 14:syntax error:unexpected end of Fi if
If
Command
Then if
Function
Then
Command executed successfully, equal to return 0 (such as grep, find a match)
Execution failed, returned non 0 (grep, no match found)
if [Expression_r_r_r]
Then the result of the expression is true, return 0,if to the 0 value then
if test Expression_r_r_r
Then the result of the expression is false, return a non-0,if to the value of a non-0 then
[] &&--Quick IF
[f "/etc/shadow"] && echo "This computer uses Shadow Passwors"
&& can be understood as then
If the expression on the left is true, execute the statement on the right. difference between if and C language if
Shell if C language if
0 is true, go then just the opposite, not 0 go then
An integer variable is not supported directly if
Must: If [I–ne 0] But support string variable direct if
If [str] If the string is not 0
Support Variable Direct IF
if (i)
With more than one command or function as if condition
Copy Code code as follows:
Echo–n "Input:"
Read user if
Multiple instructions, which are equivalent to "and" (with)
grep $user/etc/passwd >/tmp/null
Who-u | grep $user
Then the instructions above are executed successfully, the return value is $? 0,0 for true, run then
echo "$user has logged"
else instruction execution failed, $? 1, run Else
echo "$user has not logged"
Fi
# sh test.sh
Input:macg
MACG pts/0 May 15 15:55. 2075 (192.168.1.100)
MACG has logged
# sh test.sh
Input:ddd
DDD has not logged a function as an if condition (the function is the command, and the advantage of the function is that its return value can be customized)
If
Using a function as an if condition,
Getyn
Then function reture value 0 is true, go then
echo "Your answer is yes"
else function return value is not 0 false, go else
echo "Your anser is no"
The fi if command is equivalent to Command+if $?
$ VI testsh.sh
#!/bin/sh if
Cat 111-tmp.txt | grep ting1
Then
Echo found
Else
echo "No found"
Fi
$ VI testsh.sh
#!/bin/sh Cat 111-tmp.txt | grep ting1
If [$?-eq 0]
Then
echo $?
Echo found
Else
echo $?
echo "No found"
Fi
$ sh testsh.sh
No found $ sh testsh.sh
1
No found
$ VI 111-tmp.txt
This is 222file
THISTING1 is 111file $ sh testsh.sh
Thisting1 is 111file
Found
$ VI 111-tmp.txt
This is 222file
THISTING1 is 111file $ sh testsh.sh
Thisting1 is 111file
0
Found
Traditional if from sentence--conditional expression as if condition
Copy Code code as follows:
If [conditional expression]
Then
Command
Command
Command
Else
Command
Command
Fi
Conditional expression
If [f-file] If the file exists
If [-D ...] If the directory exists
If [s file] If the file exists and is not empty
If [-R File] If the file exists and is readable
If [-W file] If the file exists and can be written
If [-X file] If the file exists and is executable
If [Int1-eq Int2] if int1 equals Int2
If [Int1-ne Int2] if not equal to
If [Int1-ge Int2] If >=
If [int1-gt Int2] If >
If [Int1-le Int2] If <=
If [int1-lt Int2] If <
string-Variable expression
Copy Code code as follows:
if [$a = $b] if string1 equals string2
string allows an equal sign to be used with an assignment number
If [$string 1!= $string 2] If string1 is not equal to string2
If [-n $string] returns 0 (true) if string is Non-null (not 0)
If [-Z $string] if string is empty
If [$sting] if string is Non-null, returns 0 (and-n similar) conditional expression reference variable to take $
if [a = b]; then
echo Equal
Else
Echo No equal
Fi
[Macg@machome ~]$ SH test.sh
Input A:
5
Input B:
5
No equal (equals expression is not compared $a and $b, but compares and A and B, Nature A!=b) corrects:
if [$a = $b]; then
echo Equal
Else
Echo No equal
Fi
[Macg@machome ~]$ SH test.sh
Input A:
5
Input B:
5
Equal
-eq-ne-lt-nt can only be used for integers, not for strings, strings equal to assignment numbers =
[Macg@machome ~]$ VI test.sh
Echo-n "Input your choice:"
Read Var
If [$var-eq "yes"]
Then
Echo $var
Fi
[Macg@machome ~]$ sh-x test.sh
Input your choice:
Y
Test.sh:line 3:test:y: integer expression_r_r_r expected
Expected integer form, that is,-eq does not support string = placed elsewhere is the assignment, placed in the IF [] is the string equals, the shell does not have = =, that is the C language equals
A string without spaces, you can add "", or you can not add
[Macg@machome ~]$ VI test.sh
echo "Input A:"
Read a
echo "Input is $a"
if [$a = 123]; Then
Echo equal123
Fi
[Macg@machome ~]$ SH test.sh
Input A:
123
Input is 123
equal123 = as equals, both sides must add a space, otherwise the invalidation
The equal sign is also an operator, must and other variables, keywords, with an empty Geggai (equal sign as the assignment number is exactly the opposite, can not have spaces on either side)
[Macg@machome ~]$ VI test.sh echo "Input your choice:"
Read Var
if [$var = "yes"]
Then
Echo $var
echo "Input is correct"
Else
Echo $var
echo "Input Error"
Fi
[Macg@machome ~]$ VI test.sh echo "Input your choice:"
Read Var
if [$var = "yes"] add space on both sides of the equals sign
Then
Echo $var
echo "Input is correct"
Else
Echo $var
echo "Input Error"
Fi
[Macg@machome ~]$ SH test.sh
Input your choice:
Y
Y
Input is correct
[Macg@machome ~]$ SH test.sh
Input your choice:
N
Input is correct
Lose the wrong also walk then, all go then, why?
Because if the $var= "yes" is connected to a variable, and the variable is null and returns 1, then the else [macg@machome ~]$ sh test.sh
Input your choice:
Y
Y
Input error
[Macg@machome ~]$ SH test.sh
Input your choice:
No
No
Input error
All normal if [$ANS] is equivalent to if [-N $ANS]
If the string variable is non-null (then), null (ELSE)
echo "Input your choice:"
Read ANS if [$ANS]
then
Echo no empty
Else
Echo empth
fi
[Macg@machom E ~]$ sh test.sh
Input your choice: carriage return
empth description "Carriage return" is an empty string
[macg@machome ~]$ sh test.sh
Input your choice:
No empty
integer conditional expression, greater than, less than, Shell without > and <, will be treated as angle brackets, only-ge,-gt,-le,lt
[macg@machome ~]$ VI test.sh echo "input A:"
read a
if [$a-ge 100]; Then
Echo 3bit
Else
Echo 2bit
fi
[macg@machome ~]$ sh test.sh
Input A:
123
3bit
[ Macg@machome ~]$ sh test.sh
Input A:
2bit integer operation symbol-GE,-GT,-LE,-LT, don't forget to add-
if test $a GE 100; Then [macg@machome ~]$ sh test.sh
Test.sh:line 4:test:ge:binary operator expected
if test $a-ge 100; Then [macg@machome ~]$ sh test.sh
Input A:
123
3bit
Logic is not! The opposite of a conditional expression
if [! expression]
if [! d $num] If directory $num is not present
The juxtaposition of logic and –a conditional expressions
If [expression 1–a expression 2]
Or of a logical OR-o conditional expression
If [expression 1–o expression 2]
Logical expression
The expression is combined with the preceding =!=-d–f–x-ne-eq-lt, etc.
Logical symbols are normally followed by other expressions, without any parentheses (), which is tied
If [-Z "$JHHOME"-a-d $HOME/$num]
Note that logic and-A are easy to confuse with logical OR-o and other strings or file operators
The most common form of assignment, which evaluates the variables on both sides before assigning them
Whether the left test variable is empty, the right test directory (value) exists (value is valid)
Copy Code code as follows:
[Macg@mac-home ~]$ VI test.sh
:
echo "Input" num: "
Read num
echo "Input is $num" if [-Z "$JHHOME"-a-d $HOME/$num] If the variable $jhhome is empty and $home/$num directory exists
Then
Jhhome= $HOME/$num is assigned
Fi
echo "Jhhome is $JHHOME"
-----------------------
[Macg@mac-home ~]$ SH test.sh
Input the num:
Input is PPP
Jhhome is directory-D $HOME/$num does not exist, so $jhhome is not assigned to then
[Macg@mac-home ~]$ mkdir PPP
[Macg@mac-home ~]$ SH test.sh
Input the num:
Input is PPP
Jhhome IS/HOME/MACG/PPP A case of-O, which reveals the problem of "=" must be left blank on both sides
echo "Input your choice:"
Read ANS if [$ANS = "yes"-o $ANS = "yes"-o $ANS = "y"-o $ANS = "Y"]
Then
Ans= "Y"
Else
Ans= "n"
Fi
Echo $ANS
[Macg@machome ~]$ SH test.sh
Input your choice:
Y
[Macg@machome ~]$ SH test.sh
Input your choice:
No
Y
Why input is not yes, the result is still Y (go then)
Because = was connected to read, became the variable $ans= "Yes", and the variable is empty, so go else
[Macg@machome ~]$ VI test.sh echo "Input your choice:"
Read ANS echo "Input your choice:"
Read ANS
if [$ANS = "yes"-o $ANS = "yes"-o $ANS = "y"-o $ANS = "Y"]
Then
Ans= "Y"
Else
Ans= "n"
Fi
Echo $ANS
[Macg@machome ~]$ SH test.sh
Input your choice:
No
[Macg@machome ~]$ SH test.sh
Input your choice:
Yes
Y
[Macg@machome ~]$ SH test.sh
Input your choice:
Y
Y =================== takes the test condition expression as an if condition ===================================
if test $num-eq 0 is equivalent to if [$num –eq 0]
Test expression, no []
if test $num-eq 0
Then
echo "Try Again"
Else
echo "Good"
Fi
Mans test
[Macg@machome ~]$ Man Test
[(1) User Commands [(1) Synopsis
Test EXPRESSION
[EXPRESSION]
[-N] STRING
The length of the STRING is nonzero-n and the direct $str are all non 0 conditions
-Z STRING
The length of STRING is zero
STRING1 = STRING2
The strings are equal
STRING1!= STRING2
The strings are not equal
Integer1-eq INTEGER2
INTEGER1 is equal to INTEGER2
Integer1-ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1-GT INTEGER2
INTEGER1 is greater than INTEGER2
Integer1-le INTEGER2
INTEGER1 is less than or equal to INTEGER2
Integer1-lt INTEGER2
INTEGER1 is less than INTEGER2
Integer1-ne INTEGER2
INTEGER1 is isn't equal to INTEGER2
File1-nt FILE2
FILE1 is newer (modification date) than FILE2
File1-ot FILE2
FILE1 is older than FILE2
-B FILE
FILE exists and is block special
-C FILE
FILE exists and is character special
-D FILE
FILE exists and is a directory
-E FILE
File exists files exist
-F FILE
File exists and is a regular file exists and is a normal file
-H FILE
FILE exists and is a symbolic link (same as-l)
-L FILE
FILE exists and is a symbolic link (same as-h)
-G FILE
FILE exists and is owned by the effective group ID
-O FILE
FILE exists and is owned by the effective user ID
-P FILE
FILE exists and is a named pipe
-S FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-W FILE
FILE exists and is writable
-X FILE
FILE exists and is executable
The most commonly used simplified if statement
&& if "front", then "back"
Copy Code code as follows:
[-f/var/run/dhcpd.pid] && rm/var/run/dhcpd.pid Check if the file exists, delete it if it exists
|| If it is not "front", then the back
Copy Code code as follows:
[-F/USR/SBIN/DHCPD] | | Exit 0 verifies that the file exists and exits if it exists
Using simplified if and $1,$2,$3 to detect parameters, call help unreasonably
Copy Code code as follows:
[-Z ' $] && help if the first argument does not exist (-Z string length is 0)
["$" = "-H"] && help if the first argument is-H, show help
Example
Copy Code code as follows:
#!/bin/sh
[-F "/etc/sysconfig/network-scripts/ifcfg-eth0"] && rm-f/etc/sysconfig/network-scripts/ifcfg-eth0
CP Ifcfg-eth0.bridge/etc/sysconfig/network-scripts/ifcfg-eth0
[-F "/etc/sysconfig/network-scripts/ifcfg-eth1"] && rm-f/etc/sysconfig/network-scripts/ifcfg-eth1
CP Ifcfg-eth1.bridge/etc/sysconfig/network-scripts/ifcfg-eth1
[-F "/etc/sysconfig/network-scripts/ifcfg-eth0:1"] && rm-f/etc/sysconfig/network-scripts/ifcfg-eth0:1