Centosif judgment command

Source: Internet
Author: User
No matter what programming language, the linuxif command cannot be separated from conditional judgment. SHELL is no exception. Iflistthendosomethinghereeliflistthendoanotherthinghereelsedosomethingelseherefi #! /Bin/shSYS linux if command

No matter what programming language, conditional judgment is indispensable. SHELL is no exception.

If list
Then
Do something
Here
Elif list
Then
Do another thing
Here

Else
Do something else here
Fi


#! /Bin/sh

SYSTEM = 'uname-s' # obtain the operating SYSTEM type.

If [$ SYSTEM = "Linux"]; then
# If it is linux, print the linux string
Echo "Linux"
Elif [$ SYSTEM = "FreeBSD"];
Then
Echo "FreeBSD"
Elif [$ SYSTEM = "Solaris"];
Then
Echo "Solaris"
Else
Echo "What? "
Fi
# If end

It is basically the same as other scripting languages. There is no big difference. However, it is worth noting that. [] Condition judgment.

1 string judgment

Str1 = str2: true when two strings have the same content and length
Str1! = Str2: true when str1 and str2 are different
-N
Str1 is true when the length of a string is greater than 0 (the string is not empty)
-Z str1
True (empty string) when the string length is 0)
Str1 is true when str1 is not empty

2. digit judgment

Int1-eq int2 two numbers are equal to true
Int1-ne int2 two numbers are not true
Int1-gt
Int2 int1 greater than int2 true
Int1-ge int2 int1 greater than or equal to int2 true
Int1-lt
Int2 int1 smaller than int2 true
Int1-le int2 int1 smaller than or equal to int2 true

3. file judgment

-R file user readable to true
-W file users can write to true
-X file user executable is true
-F
The file is a regular file and is true.
-D file: The Directory is true.
-C file: The special character file is true.
-B
The file is a special block file that is true.
-S file is true if the file size is not 0
-T
File is true when the device specified by the file descriptor (1 by default) is a terminal.

3. complex logic judgment

-A and
-O or
! Non

End

Although the syntax is simple, its functions become powerful when used in SHELL.

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.