Shell Script Loop statement--until loop

Source: Internet
Author: User

Until cycle
The environment used to perform loops when a proposition is false
When a proposition is false, it enters the loop; exit the loop when the proposition is true
Or if the condition is not satisfied, enter the cycle of the scene
In logical judgment, until and while are just the opposite.

Syntax format:
Until CONDITION; Do
Loop body
Done
Entry condition: CONDITION is False
Exit Condition: CONDITION is True

Until Cycle 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 17:08:54
#Description: 1-100 all positive integer ' s sum

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

until [[$I-GT]];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:30:28
#Description:p ing all host and count the number

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

until [[$host-ge 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:44:27
#Description: Random number comparative

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


Echo $minrandom

until [[$R-GT 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=" Untilrandom.png "src=" http://s2.51cto.com/wyfs02/M02/86/36/ Wkiom1e4tbuanjk6aaa005zcgdg480.png "alt=" Wkiom1e4tbuanjk6aaa005zcgdg480.png "/>

4, print 99 multiplication table

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create time:2016-08-16 21:57:22
#Description: Multiplication tab

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

until [[$Row-GT 9]];d o
until [[$Column-gt $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=" Until99.png "src=" http://s4.51cto.com/wyfs02/M01/86/36/ Wkiol1e4s8vryl3taabb5g2txp4504.png "alt=" Wkiol1e4s8vryl3taabb5g2txp4504.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-17 22:36:44
#Description: Monitor Whether the hacker user have logged on current system

Until 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/1840663

Shell Script Loop statement--until 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.