Shell script detailed explanation (4)-while loop and until loop of loop statement
- One, until loop
- Two, while loop
-
- 1. Loop structure
- 2. Sample questions (everyone’s thinking is different, which will cause the script content to be different, here is just my personal demonstration)
-
- ① Add users in batch
- ②, guess the number game
- ③, calculate the sum of all integers from 1 to 100
- ④ Filter out the odd and even numbers of all integers from 1 to 100
- ⑤, prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number
- ⑥ Find the even and odd sums of all integers from 1 to 100
- ⑦, detect whether the host in the specified range is communicating, and output the communicating host ip to the file host_ip
- ⑧, output all executable files in the /dev directory
- ⑨, execute the script to enter the user name, if the user exists, the output prompts that the user already exists; if the user does not exist, prompt the user to enter the password, create the user and set up its password
- ⑩, output all directories contained in the environment variable PATH and all executable files in them
One, until loop
1. Loop structure
- Repeat test a certain condition, as long as the condition is not established, the loop will be repeated, and the condition will be exited from the loop
2. Examples
- Calculate the sum of all integers from 1 to 100
Two, while loop
1. Loop structure
- Repeat test a certain condition, as long as the condition is established, the loop will be repeated, and the condition will be exited from the loop (the opposite of the until loop)
2. Sample questions (everyone’s thinking is different, which will cause the script content to be different, here is just my personal demonstration)
① Add users in batch
- The user name starts with stu and is numbered in order
- Add a total of 20 users, namely stu1, stu2, stu3...stu20
- The initial password is set to 123456
②, guess the number game
- Get a random integer between 0-999 through the variable RANDOM
- Prompt the user to guess and record the number of times
- If the guessed number is too large or too small, the user will be prompted, and the guess will exit the loop
③, calculate the sum of all integers from 1 to 100