UNIX System Management: Shell Programming Branch statements (2)

Source: Internet
Author: User
Keywords Unix System Management shell programming branching statements
Tags based control control mode echo example list process process control

8.if statement

Syntax: (for one-way judgment branches)

If

List A

Then

List B

Fi

Example:

If

Test-s Funfile

Then

Echo Funfile exists

Fi

echo Hello

An If structure is a process control method based on the return value of a command. If the test command returns a value of 0, a specified list of commands is executed, and if the command returned with a value other than 0, the specified list of commands is ignored and not executed.

The example above illustrates a common format for the IF structure: Each command list consists of a shell command of one or more UNIX systems, each of which is separated by a carriage return or semicolon, and the last command executed in List a determines the result of the IF statement.

The procedure for if structure execution is as follows:

1.list a command is executed.

2. If the last command in List a returns a value of 0 (True), execute the command in List B, and then continue with the command after FI.

3. If the return value of the last command in List A is not 0 (false), skip to fi and continue with the command after FI.

The test command is often used as a process control, and it can use any UNIX command because all UNIX commands produce a return value, as the following example illustrates:

If

grep kingkong/etc/passwd >/dev/http://www.aliyun.com/zixun/aggregation/19527.html ">null

Then

Echo found KingKong

Fi

The If structure can also provide process control when a program fails. As shown in the following example:

If

[$# NE 3]

Then

echo Incorrect syntax

echo usage:cmd arg1 arg2 Arg3

Exit 99

Fi

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.