This article mainly introduces how to use if Shell script to program if statements. for details, refer to the if statement format.
If condition
Then
Command
Else
Command
Fi, don't forget the end.
If statements forget the final fi
Test. sh: line 14: syntax error: unexpected end of fi if
If
Command
Then if
Function
Then
If the command is successfully executed, 0 is returned (such as grep, matching is found)
Execution failed. non-0 (grep, no matching found) is returned)
If [expression_r_r]
If the result of the then expression is true, 0 is returned. if, the value 0 is directed to then.
If test expression_r_r_r
If the result of the then expression is false, a non-0 value is returned. if, the non-0 value is directed to 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 shell statement on the right. if is different from the C language if function.
Shell if C language if
0 is true. then is the opposite, and then is not 0.
Direct if
Required: if [I-ne 0], but string variables are supported. if
If [str] if the string is not 0
Support direct if
If (I)
Use multiple commands or functions as the if condition
Echo-n "input:" read user if multiple commands, which are equivalent to "and" (and) grep $ user/etc/passwd>/tmp/null who-u | grep $ all the commands above userthen are successfully executed. the returned value is $? If the value is 0, 0 is true, an error occurred while running the then echo "$ user has logged" else command, $? 1, run else echo "$ user has not logged" fi # sh test. shinput: macgmacg pts/0 May 15 :55. 2075 (192.168.1.100) macg has logged # sh test. shinput: dddddd has not logged uses the function as the if condition (the function is equivalent to command, and the function has the advantage that its return value can be customized) if uses the function as the if condition, the reture value of the getynthen function is 0. The return value of the thenecho "your answer is yes" else function is false if the return value is not 0, using elseecho "your anser is no" fi if command is equivalent to command + if $? $ Vi testsh. sh #! /Bin/sh ifcat 111-tmp.txt | grep ting1thenecho foundelseecho "no found" fi $ vi testsh. sh #! /Bin/sh cat 111-tmp.txt | grep ting1if [$? -Eq 0] thenecho $? Echo foundelseecho $? Echo "no found" fi $ sh testsh. shno found $ sh testsh. sh1no found $ vi 111-tmp.txtthat is 222filethisting1 is 111 file $ sh testsh. shthisting1 is 111 filefound $ vi 111-tmp.txtthat is 222filethisting1 is 111 file $ sh testsh. shthisting1 is 111file0found
Traditional if statements -- use conditional expressions as if conditions
If [conditional expression] then command commandelse command commandfi
Conditional expressions
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 can be executed
If [int1-eq int2] if int1 is equal to int2
If [int1-ne int2] is not equal
If [int1-ge int2] if> =
If [int1-gt int2] if>
If [int1-le int2] if <=
If [int1-lt int2] if <
String variable expression
If [$ a = $ B], if string1 is equal to string2, a value can be used as the equal sign. If [$ string1! = $ String2] if string1 is not equal to string2 if [-n $ string] if string is not empty (not 0), 0 (true) is returned) if [-z $ string] if string is empty if [$ sting] if string is not empty, 0 is returned (similar to-n) the variable referenced by the conditional expression must contain $ if [a = B]; then echo cancelseecho no need fi [macg @ machome ~]. $ Sh test. shinput a: 5 input B: 5no equal (equal to the expression not compared $ a and $ B, but compared with a and B, natural! = B) Correction: if [$ a = $ B]; then echo cancelseecho no 0000fi [macg @ machome ~] $ Sh test. shinput a: 5 input B: 5 equal-eq-ne-lt-nt can only be used as integers, not applicable to strings, strings are equal to the value = [macg @ machome ~] $ Vi test. shecho-n "input your choice:" read varif [$ var-eq "yes"] thenecho $ varfi [macg @ machome ~] $ Sh-x test. shinput your choice: ytest. sh: line 3: test: y: integer expression_r_r_r expected integer form, that is,-eq does not support string = to assign values elsewhere. if [], it means the string is equal, shell does not contain =, which is a string in C that equals no space. you can add "" or [macg @ machome ~]. $ Vi test. shecho "input a:" read aecho "input is $ a" if [$ a = 123]; thenecho listen 123fi [macg @ machome ~] $ Sh test. when shinput a: 123 input is 123104123 = is equal to or equal to, both sides must be added with spaces. otherwise, invalid equal signs are also operators and must be used with other variables and keywords, open with an empty lattice (the opposite is true when the equal sign is assigned, and there cannot be spaces on both sides) [macg @ machome ~] $ Vi test. sh echo "input your choice:" read varif [$ var = "yes"] thenecho $ varecho "input is correct" elseecho $ varecho "input error" fi [macg @ machome ~] $ Vi test. sh echo "input your choice: "read varif [$ var =" yes "] add a space on both sides of the equal sign thenecho $ varecho" input is correct "elseecho $ varecho" input error "fi [macg @ machome ~] $ Sh test. shinput your choice: yyinput is correct [macg @ machome ~] $ Sh test. shinput your choice: if ninput is correct fails, then is used. why? If $ var = "yes" is serialized as a variable, and this variable is null. if 1 is returned, else [macg @ machome ~] is used. $ Sh test. shinput your choice: yyinput error [macg @ machome ~] $ Sh test. shinput your choice: no noinput error everything is normal If [$ ANS] is equivalent to if [-n $ ANS] If the string variable is not null (then), null (else) echo "input your choice:" read ANS if [$ ANS] thenecho no emptyelseecho empthfi [macg @ machome ~] $ Sh test. shinput your choice: Press enter empth to indicate that "press enter" is an empty string [macg @ machome ~] $ Sh test. shinput your choice: 34no empty integer conditional expression, greater than, less than, shell does not> and <, will be treated as angle brackets, only-ge,-gt,-le, lt [macg @ machome ~] $ Vi test. sh echo "input a:" read aif [$ a-ge 100]; thenecho 3 bitelseecho 2 bitfi [macg @ machome ~] $ Sh test. shinput a: 1233bit [macg @ machome ~] $ Sh test. shinput a: 202bit integer operation symbol-ge,-gt,-le,-lt, do not forget to add-if test $ a ge 100; then [macg @ machome ~] $ Sh test. shtest. sh: line 4: test: ge: binary operator expectedif test $ a-ge 100; then [macg @ machome ~] $ Sh test. shinput a: 1233bit
Non-logical! Inverse of conditional expressions
If [! Expression]
If [! -D $ num] if the directory does not exist $ num
Parallel logic with-a conditional expression
If [expression 1-a expression 2]
Or of a logical or-o condition expression
If [expression 1-o expression 2]
Logical expression
Expression and the previous =! =-D-f-x-ne-eq-lt
Logical symbols are normally connected to other expressions without any parentheses ().
If [-z "$ JHHOME"-a-d $ HOME/$ num]
Note that logic and-a and logic or-o are easily mixed with the operator numbers of other strings or files.
The most common assignment method is to evaluate the variables = both sides before the assignment.
Whether the test variable on the left is empty, and whether the test Directory (value) on the right exists (value is valid)
[Macg @ mac-home ~] $ Vi test. sh: echo "input the num: "read numecho" input is $ num "if [-z" $ JHHOME "-a-d $ HOME/$ num] if the variable $ JHHOME is empty, if the $ HOME/$ num directory contains thenJHHOME = $ HOME/$ num, the value is fiecho "JHHOME is $ JHHOME" --------------------- [macg @ mac-home ~]. $ Sh test. shinput the num: input is pppJHHOME is Directory-d $ HOME/$ num does not exist, so $ JHHOME is not assigned a value by then [macg @ mac-home ~] $ Mkdir ppp [macg @ mac-home ~] $ Sh test. shinput the num: input is pppJHHOME is/home/macg/ppp-o example, which reveals the question "=" the two sides must leave spaces echo "input your choice: "read ANS if [$ ANS =" Yes "-o $ ANS =" yes "-o $ ANS =" y "-o $ ANS =" Y "] thenANS =" y "elseANS =" n "fiecho $ ANS [macg @ machome ~] $ Sh test. shinput your choice: y [macg @ machome ~] $ Sh test. shinput your choice: Why is the input of noy not yes, and the result is still y (by then) Because = is serialized, it becomes the variable $ ANS = "Yes", and the variable is empty, so I took else [macg @ machome ~] $ Vi test. sh echo "input your choice:" read ANS echo "input your choice: "read ANSif [$ ANS =" Yes "-o $ ANS =" yes "-o $ ANS =" y "-o $ ANS =" Y "] thenANS =" y" elseANS = "n" fiecho $ ANS [macg @ machome ~] $ Sh test. shinput your choice: no [macg @ machome ~] $ Sh test. shinput your choice: yesy [macg @ machome ~] $ Sh test. shinput your choice: yy ============================ use the test condition expression as the if condition ================== ================================== if test $ num-eq 0 is equivalent to if [$ num-eq 0] test expression, no [] if test $ num-eq 0 thenecho "try again" elseecho "good" fi man test [macg @ machome ~] $ Man test [(1) User Commands [(1) SYNOPSIS test EXPRESSION [EXPRESSION] [-n] STRING the length of STRING is nonzero-n and $ str are both non-0 conditions-z STRING the length of STRING is zero STRING1 = STRING2 strings are equal STRING1! = Beyond the strings are not equal INTEGER1-eq INTEGER2 INTEGER1 is equal to INTEGER2 INTEGER1-ge INTEGER2 contains is greater than or equal to INTEGER2 INTEGER1-gt INTEGER2 INTEGER1 is greater than INTEGER2 integer-INTEGER2 plugin is less than or equal to INTEGER2 INTEGER1-lt INTEGER2 INTEGER1 is less than INTEGER2 INTEGER1-ne INTEGER2 INTEGER1 is not equal to INTEGER2 FILE1-nt FILE2 FILE1 is newer (modification date) than FILE2 FILE1-ot FILE2 FILE1 is older than FILE2-B FILE exists and is block special-c FILE exists and is character special-d FILE exists and is a directory-e FILE exists FILE-f FILE file exists and is a regular FILE exists and is a common FILE-h FILE exists and is a symbolic link (same as-L) -l file exists and is a symbolic link (same as-h) -g file exists and is owned by the specified tive group ID-o file exists and is owned by the specified tive user ID-p FILE exists and is a named pipe-s FILE exists and has a size greater than zero-s file exists and is a socket-w FILE exists and is writable-x FILEFILE exists and is executable
The most common simplified if statement
& If it is "front", then "back"
[-F/var/run/dhcpd. pid] & rm/var/run/dhcpd. pid check whether the file exists. If yes, delete it.
| If it is not "front ",
[-F/usr/sbin/dhcpd] | exit 0 checks whether the file exists. If yes, exit
Use simplified if and $1, $2, and $3 to detect parameters. if it is unreasonable, call help.
[-Z "$1"] & help if the first parameter does not exist (-z string length is 0) ["$1" = "-h"] & help
Example
#!/bin/sh[ -f "/etc/sysconfig/network-scripts/ifcfg-eth0" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0cp ifcfg-eth0.bridge /etc/sysconfig/network-scripts/ifcfg-eth0[ -f "/etc/sysconfig/network-scripts/ifcfg-eth1" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth1cp 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
For more articles about how to use the if statement for Linux shell script programming (conditional judgment), please follow the PHP Chinese network!