Shell conditional expression

Source: Internet
Author: User

F 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

Three conditional expressions of 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]
Then
If the expression is true, 0 is returned. If, the value 0 is directed to then.
If test expression_r_r_r
Then
If the expression result is false, non-0 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 Statement on the right.

 

Functional Differences Between shell if and C if

Shell if C language if
0 is true, go to then On the contrary, if the value is not 0, then is used.
Direct if
Required: If [I-Ne 0]

However, if
If [STR] If the string is not 0

Support direct if
If (I)

 
====================================== Use command as the if condition ==== ======================================

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 $ user
Commands on thenBothExecution successful, return value $? 0,0 is true, Run then
Echo "$ user has logged"
Else command execution failed, $? 1, run else
Echo "$ user has not logged"
Fi

# Sh test. Sh
Input: MACG
MACG pts/0 May 15 :55. 2075 (192.168.1.100)
MACG has logged

# Sh test. Sh
Input: ddd
Ddd has not logged

Use a 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
Use a function as the if condition,
Getyn
The value of Reture in the then function is 0.
Echo "your answer is yes"
The Return Value of the else function is false if it is not 0.
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 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
That is 222 File
Thisting1 is 111 File

$ Sh testsh. Sh
Thisting1 is 111 File
Found

$ VI 111-tmp.txt
That is 222 File
Thisting1 is 111 File

$ Sh testsh. Sh
Thisting1 is 111 File
0
Found


========================================================== Use a conditional expression as the if condition ====================================

Traditional if statements -- use conditional expressions as if conditions
If [conditional expression]
Then
Command
Command
Command
Else
Command
Command
Fi

Conditional expressions

  • 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 can be executed

  • Integer variable expression

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 string can be equal by a value.
If [$ string1! = $ String2] If string1 is not equal to string2
If [-N $ string] If the string is not empty (not 0), 0 (true) is returned)
If [-Z $ string] is null
If [$ Sting] If the string is not null, 0 is returned (similar to-N)

Variable referenced by conditional expressions must contain $

If [A = B]; then
Echo equal
Else
Echo no equal
Fi
[MACG @ machome ~] $ Sh test. Sh
Input:
5
Input B:
5
No equal (equal to the expression that does not compare $ A and $ B, but compares with a and B, natural! = B)

Correction:

If [$ A = $ B]; then
Echo equal
Else
Echo no equal
Fi
[MACG @ machome ~] $ Sh test. Sh
Input:
5
Input B:
5
Equal


-Eq-ne-lt-NT can only be used as an integer. It is not applicable to strings. A string equals to a value =

[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, I .e.-EQ does not support strings

= The value is assigned elsewhere. If [] is the string equals. Shell does not contain =, which is equal to the C language.

A string without spaces. You can add "" or no space.

[MACG @ machome ~] $ VI test. Sh
Echo "input :"
Read
Echo "input is $"
If [$ A = 123]; then
Echo processing 123
Fi
[MACG @ machome ~] $ Sh test. Sh
Input:
123
Output is 123
2017123

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.