Shell (2) condition 2.16.doc

Source: Internet
Author: User

Used
If/while
And so on;

 

Test
Command

Test Condition
, When the condition is
True
Returns
0
;

If
Test condition; then
If
[Condition]; then
#

Note the space,
TBD

Supported?
If 0


...
<=>
...

Fi
Fi

 

Test
Condition judgment, three types

1

) Value Comparison

-Eq-ge-GT-le-lt-ne

 

2

) String comparison:

=
! =
<> (
Note escape
)-N (
Non-empty string
)-Z (
Empty string or not
)
;

Test
Uppercase and lowercase letters are the same
Sort
Opposite

 

Eg: If ["A"/> "B"]; then

Eg: If [-n "$1"]; then
<=> ["$1"]
#$1
It must be caused. Otherwise, it will fail to be judged.

Eg: If [string1-A string2]; then
# String1/string2
True

Eg: If [string1-O string2]; then # string1
Or
String2
True

 

3

) File comparison:

-D File
# Directory,
File
Is it a directory?

-E
# Exist,
File
Exist?

-F
# File
,
An exist File

-R
# Readable

-S
# Not
Empty

-W
# Writable

-X
# Executable
, And
Current User
You must have the execution permission.

-O
# Owered
By current user

-G
#
The default file user group is
Current User Group

File1-nt file2
# File1 new than file2

File1-ot file2
# Old

The above comparison will return a failure if the file does not exist. Therefore, you can determine whether the file exists before executing other comparisons;

 

Composite condition comparison

Similar
C
:
[Cond1] & [cond2] | [
Cond3]

 

If [
'A'/<'B'] & ['A'/<"C"] | ['A'/<'A'
]; Then

 

Advanced features:

A mathematical expression in a double parentheses table. It can also be used for mathematical computation,
If ($ A ** 2> 90 ))

The regular expression can be used to provide the pattern matching function,
If [[$ A = r *]

 

TBD
: How does one express non-logical?

 

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.