Shell programming Conditional----Use if ... then----multiple

Source: Internet
Author: User

Conditional judgment----Use if ... then----multiple

In the judgment of the same data, what should be done if the data needs to be judged in many different ways? For example, in the sh06.sh script above, we just have to make a $yn judgment (only once if) and don't want to make multiple if judgments. At this point you need to know the following syntax:

# A conditional judgment, sub success and failure (else)

If [conditional judgment]; Then

When the conditional judgment is established, the instruction work content can be carried out;

Else

When the conditional judgment is not established, the instruction work content can be carried out;

Fi

If you consider a more complex situation, you can use this syntax:

# Multiple conditional judgments (if ... else) are performed in a number of different situations (if elif ... elif)

If [conditional judgment type one]; Then

When the conditional judgment is established, the instruction work content can be carried out;

elif [conditional judgment type II]; Then

When the conditional judgment two is established, the instruction work content can be carried out;

Else

When the conditional judgment one and two are not established, the instruction work content can be carried out;

Fi

What you have to note is that Elif is also a judgment, so the appearance of elif will be followed then to deal with! But else is the last result of the failure, so else there is no then!

The source program is as follows:

1#!/bin/Bash2 #..........3Echo"test start or stop"4 #test Services5Var_ftp= '/usr/bin/pgrep" $"`6 if["$var _ftp"!="" ];7  Then8Echo"Service is running!"9 ElseTenEcho"service is not running!" OneFi

Create the shell source program with the VI editor:

Enter (enter) to write the shell source program with the VI editor:

The results of the implementation are as follows:

Shell programming Conditional----Use if ... then----multiple

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.