Shell programming-loop statement-for loop, while loop and until loop


One, for loop statement

  • In our actual working environment, repetitive tasks are often carried out, and each time the objects that need to be processed are different.
  • For example, supermarket cashier
  • How can we face such simple tasks more efficiently? This is the for loop statement we will learn next

1. Structure

  • Specify a variable and value list, read different variable values, used to execute the same group of commands one by one, until the variable value runs out and exit the current loop
  • The value list here is the execution condition of the for statement, which includes multiple objects with the same attributes, which need to be specified in advance
#语法结构:
for 变量名 in 取值列表
do
        命令序列
done
  • Pay attention to spaces in the above statement structure!
  • The sequence of commands between do···done is called "loop body"
    mark
  • As shown in the figure above, the execution process is:
    • First assign the first value in the value list to the variable, and then execute the sequence of commands in the loop body
    • Then assign the second value to the variable, and execute the loop command and so on until the value runs out
    • Finally jump to the done statement, which means the end of the loop

2. Application examples

2.1 Use for to add users in batches

  • Create a new file and store the username
    mark
    mark
    mark
    mark
  • Let's delete users in batches again to deepen the impression
    mark
    mark

2.2 Check the host status based on the IP address list

  • Detect network connectivity in the value list of the 192.168.126 network segment
    mark
    mark
  • In the above script, the if statement is used in the do···done loop
  • In fact, if, for and various other shell scripts can be nested

2.3 Classic! Calculate the sum of numbers 1~100 with a loop statement

mark
mark

  • Another way
  • The usage here has the same meaning as above
    mark
    mark

2.4 Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

  • Simple~
    mark
  • Pay attention to the usage of "+="
    mark
    mark

2.5 Find the even and odd sums of all integers from 1 to 100 (until the number entered by the user)

mark
mark


Two, while loop statement

  • The for statement is suitable for occasions where the list object is irregular and the source of the list is fixed
  • What should we do if we need to control the number of cycles, the number of operation objects in numerical order, and the repeated operations according to specific conditions.
  • How is it, does it feel like a lottery, buying a lottery ticket
  • If there is a demand, there will be a response solution. This is the while loop statement we will learn next

1. Structure

  • Test a certain condition repeatedly, and execute repeatedly as long as the condition is satisfied, until the condition is no longer satisfied
  • According to the above situation, you should avoid the infinite loop, otherwise the following instructions will not be executed
  • Therefore, the command sequence in the loop body should include a statement to modify the test condition, so that the test condition no longer holds at an appropriate time, thus ending the loop
#语句结构
while 条件测试操作
do
        #命令序列
done

##示例
while 未猜中正确价格
do
        #反复猜测商品价格
done

mark

  • First judge the conditional test operation, if the condition is satisfied, execute the command sequence in the do···done loop body; return to while and then judge···This reciprocating loop until the conditional test operation after the while is not established, and finally jump to done End loop

2. Application examples

2.1 Add users with regular numbers in batches

mark
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-xy2RNtJL-1608469869870)(14CD4AEA4ECA4147B4AEF577405215DD)]

  • Let's delete it in batches
    mark
    mark

2.2 Guess the price game

  • Prompt the user to guess and record the number of guesses, after the guess is correct, the loop can exit
  • A random integer can be obtained through the environment variable RANDOM, and the remainder of it and 1000 can be calculated to obtain a random price ranging from 0 to 999
    [External link image transfer failed, the source site may have anti-leeching mechanism, it is recommended to save the image and upload it directly (img -1qJ2eez8-1608469869871)(B2C666B3AF6941B692B066129383DB65)]
  • exit 0 Return to "0", and exit the current loop after the test is completed
    [External link image transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the image and upload it directly (img-ZI4vDGaK-1608469869871)(24B8EE95626F47BBB9FDDE537B3EA8E4)]

2.3 Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

mark
mark

  • The logic of the loop body in the above picture may not be very clear to everyone. The first is: sum=0+1=1, i=1, and then the next loop, sum=1+2=3, i=2; sum=3+ 3=6 (1+2+3), i=3, loop until the user enters the number, the following let i++ is the condition that can be combined together and finally out of the loop
  • Exercise more logical thinking and apply flexibly. There are many ways to achieve this. The author is also a beginner, and everyone will make progress together.

2.4 Prompt the user to enter an integer less than 100, and calculate the even and odd sums of all integers from 1 to this number

mark
mark


Three, until loop statement

1. Structure

  • Repeatedly test a certain condition, if the condition is not established, repeatedly execute
语句结构:
until 条件测试操作
do  
   命令序列
done
  • It is the opposite logic to while, and it is less in actual use, because everyone is generally positive logic

2. Application examples

  • Calculate the sum of all integers from 1 to 100
    mark
    [External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-ooQOE06H-1608469869874)(BADE30E5EA0A4426A92413267319E0F4)]
  • How? Is it exactly the same? That is, the first sentence of the sentence structure has changed.
  • If the condition of i is greater than the number entered by the user is not established, it will be executed repeatedly, that is, if i is less than the number entered by the user, it will be executed repeatedly.

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111462846