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