Shell script (three) while loop statement

Source: Internet
Author: User

Theoretical part:

The loop means that the program executes some statements repeatedly;

Whiler loop is a kind of cyclic structure, when it is not known in advance how many times the loop executes, it is necessary to use the while loop;

while the process of running a loop statement

When using a while loop statement, you can execute a command repeatedly based on a specific condition until the condition is not satisfied;

while The process of a dead loop occurs, so the command sequence inside the loop should include a statement that modifies the test condition

while syntax structure of the statement:

Attention:

When using a while loop statement, there are two special conditional test operations that are true (true) and False (false);

Using true to indicate that the condition is always set, the sequence of commands in the loop will be executed indefinitely unless the script is forced to terminate or exit with exit;

Conversely, when false is used, the loop body will not be executed; ' These two special conditions can also be used in the condition test of the IF statement

Application examples

Users who perform bulk add specifications by using the while statement

Stu 1 Stu 2 .....

We can also use while to bulk add users, but only if the user's name must be canonical, or otherwise cannot be created using while

Vim useradd.sh

#!/bin/bash
User= "Stu"
I=1
??? While [$i-le 20]
Do
??? Useradd? ${user} $i
??? echo "123456" | passwd--stdin ${user} $i &>/dev/null
??? echo "${user} $i Users to add Success"
? Let i++
Done

First, define a user name with a prefix of Stu

Define a variable to change the condition

while when $i is less than or equal to 20, execute the following command sequence let i++ represents the value of I at once per loop plus 1

This test condition is not valid when looping to 21 times because 21 is not less than or equal to 20 so it is transferred directly to the done end loop.

Results of execution

Inspection:

You can also use the while loop to delete

Vim userdel.sh

#!/bin/bash
User= "Stu"
I=1
??? While [$i-le 20]
Do
??? Userdel-r?? ${user} $i &>/dev/null
??? echo "${user} $i Users to del Success"
? Let i++
Done

Execution results

Combat 2

The main design ideas for the appeal request are as follows

if Random integers of less than 26 are obtained by random variables, and the remainder of the 1000 is calculated to obtain the random price of 0~999, and the repeated operation can be tested by true as the test condition, and the user guesses the actual price. The process of judging the price and the actual price is implemented using the IF statement, nested within the while loop using variables to calculate the number of guesses


For example:

Vim caijiage.sh

#!/bin/bash
price=$ (expr $RANDOM% 1000)
Times=0
echo "The prices of the goods is 0-999,can your guess what?"
While True
Do
???? Read-p "Please enter your Price:" INT
??? Let times++
? If? [$INT-eq $price]
?? Then
????? echo "Good you guessed it"
????? echo "You guess the total $times times"
????? Exit 0
?? Elif
???? [$INT-gt $price]
?? Then
??? echo "is too high"
?? Else
??? echo "It ' s Too low"
? Fi
Done

The result of executing the script

Learn for while you can already write some scripts to complete simple system administration tasks.

Shell script (three) while loop statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.