If Judgment statement in Shell

Source: Internet
Author: User

If condition test Type: File test, digital test, string test

If statement structure:

    • If ...; Then ...; Fi
    • If ...; Then ...; else ...; Fi

Conditional test expression:

    • [Expression]
    • [[Expression]]
    • Test expression

Digital test: Note

    1. Only integer tests are possible
    2. if test 3-eq $V 2 equivalent to If [3-eq $V 2]

"3-eq $V 2" equals

"3-ne $V 2" range

"3-lt $V 2" is less than

-le less than or equal to-gt greater than-ge greater than or equal

String test:Note

    1. When you want to make a decimal comparison, you cannot use the method of the number test, use a string test
    2. String comparison in Shell is only =, no = =
    3. String testing is best done using "" to bring up strings

[[$V = $v]] Pattern match if the 4 starts with true

[[$V = "["]] character match if $v equals to True

[-Z "$V"]-Z means the string is empty

[-N "$V"]-n means the string is not empty

File test:

-e file Test files exist

-F file Test is a normal file

-D FILE Test Specifies whether the path is a directory

。。。

A good solution to writing a case:

    • To determine the NFS version

For V in ...

Lockp=lockd

[$V = []] && LOCKP=NFSD

SSH $SERVER $lsLocks | grep $LOCKP

Done

1. Package the lock, then use SSH to execute command 2. Use simplified if judgment

    • Judging kernel version

If Judgment statement in Shell

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.