UNIX System Management: Shell Programming Branch statements (1)

Source: Internet
Author: User
Keywords Unix System Management shell programming branching statements
Tags aliyun echo error expression html http programming return

By completing this chapter, you will be able to do the following things:

Describes the effect of a return value in a conditional branch statement.

Use the test command to parse the return value of a command.

Use the IF and case structures in a shell program.

&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp;1. return value

Shell variables? To save the return value of the last executed command:

0: Command executed successfully (TRUE)

Non 0: The command was terminated due to an error (false)

Example:

$ true $ false

$ echo $? $ echo $?

0 1

$ LS $ cp

$ echo $? USAGE:CP F1 F2

0 CP [-r] F1 ... fn d1

$ echo $? $echo $?

0 1

$echo $?

0

All commands at the end of the UNIX operating system produce a return value. This return value is usually used to determine whether the command ended normally (return 0) or an error (returns a value other than 0). You can also see what is wrong by returning a value other than 0. For example, a syntax error usually returns 0 for the 1,true command, and the false command returns 1.

Most of the judgment statements in the shell program are controlled by the process by analyzing the return value. A special variable is defined in the shell? "is used to save the return value at the end of the previous command.

You can observe the return value of the previous command in the following ways:

echo $?

When you perform a conditional judgment (less than, greater than, equal to), the return value indicates whether the condition is true (returns 0) or False (returns non-0).

The conditional judgment statement is described in the next sections.

2.test command

Syntax:

Test expression or [expression]

The test command tests the expression and sets the return value.

Value return value of expression

True 0

False not 0 (usually 1)

The test command is capable of testing the following objects:

Integer

String

File

The test command is used to evaluate an expression and produce a return value. It makes a logical expression with parameters and evaluates the return value of the expression, the test command does not produce standard output, you must judge the result of the test command by the return value, and if the expression is true, the return value is 0, and if the expression is false, the return value is 1.

The test command can be used alone, and then you can see the return value, but it is used most often in the if and while structures to provide conditional flow control.

The test command can also be replaced with [expression]. This approach can improve readability, especially when dealing with numbers or strings.

Note: You must have spaces around the [and] symbols.

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.