Shell Programming--if Statement If-z-n-f-eq-ne-lt__ programming

Source: Internet
Author: User

The use of conditional expressions in shell programming


If condition
Then
Command
Else
Command
Fi, don't forget this ending.
If statement forgets the end fi
Test.sh:line 14:syntax error:unexpected End of fi



Three conditional expressions of 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, then return 0,if the value of 0 to then
if test Expression_r_r_r
Then
If the expression result is false, it returns a 0,if to lead a non-0 value to then



[] &&--Quick IF

[f "/etc/shadow"] && echo "This computer uses Shadow Passwors"
&& can be understood as then
Executes the statement on the right if the expression on the left is true



The functional differences between the IF and C language of the shell

Shell if C language If
0 for Real, go then On the contrary, not 0 go then
An integer variable is not supported directly if
Must: If [I–ne 0]

But supporting string variables directly if
If [str] If the string is not 0
Support Variable Direct IF
if (i)



================================= takes command as an if condition ===================================

With more than one command or function as if condition

echo–n "input:"
Read user

If
Multiple instructions, which are equivalent to "and"
grep $user/etc/passwd >/tmp/null& NBSP;    &NBSP
Who-u | grep $user
then               The instructions above are executed successfully, the return value $? is 0,0 true, run then
 echo "$user has logged"
else       command execution failed, $? 1, run Else                            &NBSP
 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



function as if condition (function is equivalent to command, the advantage of 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"
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 t Ing1

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 are 111file

$ sh testsh.sh
Thisting1 is 111fi Le
found
$ VI 111-tmp.txt
This is 222file
Thisting1 is 111file

$ sh testsh.sh
Thisti NG1 is 111file
0
found



======================================== the conditional expression as an if condition =============================

Traditional if from sentence--conditional expression as if condition
If [conditional expression]
Then
Command
Command
Command
Else
Command
Command
Fi

Conditional expression File 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 an integer variable expression 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

if [$a = $b] if string1 equals string2
string allows an equal sign to be used with an assignment number
If [$string 1!= $string 2]

Related Article

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.