Shell programming example-implement Accumulation

Source: Internet
Author: User

The basic structure of until is:

Until [condition]; do

Logic

Done

The code I implemented is as follows:

Sum = 0; n = 3; in = 0;
Until ["$ n" = 0]; do
Read in
Sum = 'expr $ sum + $ in'
N = 'expr $ n-1'
Done
Echo $ sum

The basic structure of a for loop is as follows:

For Condition

Do

Logic

Done

The code I implemented is:

Sum = 0; in = 0;
For (I = 1; I <4; I ++ ))
Do
Read in
Sum = 'expr $ sum + $ in'
Done
Echo $ sum

While Loop

The basic structure of the while loop is:

While [condition]; do

Logic

Done

The code I implemented is as follows:

Sum = 0; in = 0; n = 3;

While [$ n! = 0]; do

Read in
Sum = 'expr $ sum + $ in'
N = 'expr $ n-1'

Done

Echo sum


Pay attention to the following points when writing shell code:

1. To understand the meaning of the $ symbol, it is actually the process of value. It is equivalent to getElementById in js. It is used only when reading the value of the variable, do not include this symbol when writing or assigning values to variables. Otherwise, the meaning is to write data to a constant string. This will report an error.

2. Pay attention to the format of condition judgment. The condition judgment statements are written through [], and there must be spaces between the statements and parentheses.

3. Note that the value assignment statement cannot contain spaces. Otherwise, the system regards the variable as a directory structure.

4. Pay attention to the writing of quotation marks.

5. expr expression format. Note that there must be spaces between operators and operands.


So far

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.