UNIX System Management: Shell Programming Loop statements (1)

Source: Internet
Author: User
Keywords Unix System Management shell programming looping statements
Tags based code control echo expression key list objective

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

Use the while statement to repeatedly execute a piece of code when the condition is true.

Use the loop statement to repeatedly execute a piece of code until the condition is true.

Use the interactive for statement for loop control.

1. A brief introduction to the cycle

Target: Repeats a list of commands.

control; a return value based on a key command.

Three forms: While ... do

Loop ... do

For ... do

Loop statements allow you to repeat a list of commands, and decide whether to continue the loop or to jump out of the loop is based on the return value of a command. The test command is often used to control whether a loop continues.

Unlike branching statements, the keyword that starts a branch statement in a branch statement is the opposite of the keyword that ends a branch statement (IF/FI and CASE/ESAC), the loop statement starts with a keyword and some conditions, and the loop body is surrounded by a do/done.

2. Use let for arithmetic calculation

Syntax:

Let expression or ((expression))

Example:

$ x=10 $ x=12

$ y=2 $ let "x <10"

$ let x=x+2 $ echo $?

$ echo $x 1

$ (x > 10)

$ let "x = x/(y+1)" $ echo $?

$ echo $x $0

4 $ if ((x > 10))

$ ((x = x + 1)) > then echo x Greater

$ echo $x > else echo x not greater

5 fi

X Greater

Loop statements typically use a growing number of variables to control. Using the Let command, you can use an arithmetic expression in a shell script. This command allows long integer operations to be used. In the example above, expression represents an arithmetic expression for a shell variable and an operator that can be recognized by the shell, and (()) can be used for the Let command. The expressions that the shell can recognize are as follows:

Operator description

-Minus

! Logical opposite

*/% multiply, divide, remainder

+-Plus, minus

<= >= < > relation comparison

= =!= equals Not equal to

= Assignment

Parentheses can be used to change the order of computation in an expression, as in

Let "x=x/(y+1)"

In the same

Note that double quotes are used to ignore the special meaning of parentheses. Also, if you want to use spaces to separate operators and operators, you must use double quotes, or (()) Statements:

Let "x = x + (Y/2)" or ((x= x+ (Y/2))

When using the logic and relational operators (!,<=,>=,<,>,++,~=), the shell returns a code variable that reflects whether the result is true or false, and once again, you must use double quotes to prevent the shell from treating the greater-than and less-than operators as i/ o redirect.

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.