Shell Script Loop statement--while loop

Source: Internet
Author: User

While loop
Suitable for environments where the proposition is true when the loop is executed
When a proposition is true, it enters a loop, and a proposition is a false exit loop.
Or when the condition is met, it goes into the loop.

Syntax format:
While CONDITION; Do
Loop body
Done

CONDITION: Cyclic control conditions
Entry condition: condition is true;
Exit Condition: Condition is false
Before entering the loop, make a judgment first, each time after the cycle will be judged again; the condition is "true", then a loop is executed, until the condition test state is "false" terminates the loop
Therefore: Condtion generally should have a cyclic control variable, and the value of this variable will be continuously corrected in the loop body

While loop application Example:

1, computes the and of all positive integers between 1-100

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 16:59:54
#Description: 1-100 all positive integer ' s sum

Declare-i sum=0
Declare-i I=1

while [[$I-le]];d o
Let sum+= $I
Let i++
Done
echo "1-100 all positive integer ' sum: $sum"

2, automatic scanning 1-254 network segment of all hosts, statistics on the number of online hosts and the number of non-online hosts

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 16:10:39
#Description:p ing all host and count the number

Declare-i online=0
Declare-i offline=0
Declare-i host=1

while [[$host-LT 255]];d o
Ping-c 1-w 1 10.1.250. $host &>/dev/null && echo "10.1.250. $host is Online" && let online= $online +1 | | Let offline= $offline +1
Let host= $host +1
Done
echo "Online hosts number: $online"
echo "Offline hosts number: $offline"

3, generate 10 random numbers and find the maximum random number and minimum random number

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 20:22:58
#Description: Random number comparative

Minrandom= $RANDOM
Maxrandom= $minrandom
Declare-i r=1
Echo $minrandom

while [[$R-le 9]];d o
rd= $RANDOM
Echo $RD
if [[$RD-gt $maxrandom]];then
Maxrandom= $RD
elif [[$RD-lt $minrandom]];then
Minrandom= $RD
Fi
Let r++
Done
echo "Minrandom is: $minrandom"
echo "Maxrandom is: $maxrandom"

650) this.width=650; "title=" Whilerandom.png "src=" http://s5.51cto.com/wyfs02/M00/86/36/ Wkiol1e4rulsyjtbaaazwxnku48009.png "alt=" Wkiol1e4rulsyjtbaaazwxnku48009.png "/>

4, print 99 multiplication table

#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Contact qq:183530300
#Create time:2016-08-16 20:12:30
#Description: Multiplication table

Declare-i row=1
Declare-i Column=1

While ["$Row"-lt];d o
While ["$Column"-le "$Row"];d o
Echo-ne "${column}x${row}=$[$Column * $Row]\t"
Let column++
Done
Column=1
Let row++
Echo
Done

650) this.width=650; "title=" While99.png "src=" http://s4.51cto.com/wyfs02/M02/86/36/ Wkiom1e4rncrgma1aabczta1uby733.png "alt=" Wkiom1e4rncrgma1aabczta1uby733.png "/>

5, every 3 seconds to query the system has logged on user information, if found hacker user login, log in log time and host records in/var/log/login.log, and prompts hacker users to exit the system

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create time:2016-08-18 08:14:07
#Description: Monitor Whether the hacker user has LoggedOn

While! (who | grep "^hacker\>");d O
Echo-e "' Date +%t ' \thacker user doesn ' T logged on"
Sleep 3
Done
Echo-e "' Date +%t ' \thacker user logged on" | Tee/var/log/login.log
echo "Please exit the current system" | Write hacker

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1840655

Shell Script Loop statement--while loop

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.