Shell script detailed explanation (4)-while loop and until loop of loop statement (additional examples and analysis)

Shell script detailed explanation (4)-while loop and until loop of loop statement

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

Insert picture description here

2. Examples

  • Calculate the sum of all integers from 1 to 100

Insert picture description here

Insert picture description here

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)

Insert picture description here

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

Insert picture description here

Insert picture description here

②, 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

Insert picture description here

Insert picture description here

③, calculate the sum of all integers from 1 to 100

Insert picture description here
Insert picture description here

④ Filter out the odd and even numbers of all integers from 1 to 100

Insert picture description here

Insert picture description here

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

Insert picture description here

Insert picture description here

⑥ Find the even and odd sums of all integers from 1 to 100

Insert picture description here

Insert picture description here

⑦, detect whether the host in the specified range is communicating, and output the communicating host ip to the file host_ip

Insert picture description here

⑧, output all executable files in the /dev directory

Method 1: while loop plus case condition judgment (research results by yourself, it may be more troublesome, not optimized for the time being)

Insert picture description here

Method 2: Use while read line loop plus case condition judgment

Insert picture description here

⑨, 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

Method 1, while loop plus if condition judgment (do it yourself, it may be more troublesome, it is not optimized for the time being)

Insert picture description here

Method two, while read line loop plus if condition judgment (may be more troublesome, not optimized for the time being)

Insert picture description here

⑩, output all directories contained in the environment variable PATH and all executable files in them

Insert picture description here

Guess you like

Origin blog.csdn.net/Lucien010230/article/details/114458663