Shell programming-flow control-while loop and until loop

1. while loop

  • The while loop is an indefinite loop, also called a conditional loop. As long as the judgment formula is established, the cycle will continue until the conditional judgment formula is not established, the cycle will not stop. This is not the same as the for fixed loop.

Syntax format:

while [ 条件判断式 ]
	do
		程序
	done

Give a chestnut:

  • (1) Add from 1 to 100 and calculate the result of dividing them. Start writing the script.
    Insert picture description here
    (2) Start the test. The result is correct.
    Insert picture description here

2. until loop

  • The until loop is the opposite of the while loop. In the until loop, as long as the condition is not established, the loop will be executed and the loop program will be executed. Once the loop condition is established, the loop is terminated.

Syntax format:

until [  条件判断式 ]
	do
		程序
	done

Give a chestnut:

  • (1) Add from 1 to 100 and calculate the result of dividing them. Start writing the script. In fact, this chestnut is the same as the while chestnut, except that while is changed to until, and -le is changed to -gt.
    Insert picture description here
    (2) Start the test. The calculated result is 5050
    Insert picture description here

3. Summary

  • (1) While is more suitable for non-quantitative loops (do not know the number of loops), and perform conditional loops. While, until, for these three loops can be rewritten each other, but some grammatical structures are more suitable for some for loops, and some are more suitable for while loops, depending on the application scenario.
  • (2) Scripting language, the advantage of scripting language is what you see is what you get, and the finished scripting language does not need to be compiled (equivalent to the idea is automatically compiled for you when writing Java, as long as it is run), it will run directly , It’s not that you don’t need to compile, but compile at the same time of execution. The omission simplifies the compilation process. The advantage is that the programming is simpler, but the disadvantage is that the efficiency will be slower. It is slower than the language that C needs to be compiled in execution than Java.
  • (3) Such a language is not suitable for a large number of data calculations. The biggest advantage of shell scripts is to help administrators reduce repetitive operations, or to help administrators perform system operation and maintenance work. ** Through the chestnuts of If and for, for example, determine whether the apache service is started. If it does not start, start the apache service through the script, and then record the start in the log. These scripts are executed through timed tasks. for loop, batch add users, batch delete users.
  • (4) Shell basics: Shell basic functions, definition of shell variables, definition of shell environment variables, shell regular expressions, test statements, and then the basic process. The basic content of the language. If you are proficient, writing some small scripts in your work is fine.

4. Build programming ideas

(1) Deepen the understanding of Linux, increase the amount of Linux practice, and understand what we need programs to help us do, and then come back to write programs, you will find that it is easier.

(2) The most important thing is to learn, practice, and accumulate . Computer technology is not learned, but practiced. Explains the importance of contact.

Guess you like

Origin blog.csdn.net/weixin_46818279/article/details/107697645