While loop usage

Source: Internet
Author: User
Tags rsync

While loop


1. While conditional sentence

Grammar

While condition

Do

Instruction Set

Done

###########################

2. Until conditional sentence

Utill conditions

Do

Instruction Set

Done


##########################

Example 1

Check the load of the system every 5 seconds

[Email protected] 02]# VI while.sh

#!/bin/sh

While True

Do

Uptime >> Uptime.log

Sleep 5

Done

[Email protected] 02]# TAILF Uptime.log

22:46:21 up 8:52, 5 users, Load average:0.00, 0.00, 0.00

22:46:22 up 8:52, 5 users, Load average:0.00, 0.00, 0.00

22:46:26 up 8:52, 5 users, Load average:0.00, 0.00, 0.00

22:46:27 up 8:53, 5 users, Load average:0.00, 0.00, 0.00

22:46:31 up 8:53, 5 users, Load average:0.00, 0.00, 0.00

22:46:32 up 8:53, 5 users, Load average:0.00, 0.00, 0.00

22:46:36 up 8:53, 5 users, Load average:0.00, 0.00, 0.00

=======================================

Example 2: Calculate the sum of 1+2+3+...+n, n is a parameter and can be arbitrarily specified

[email protected] 02]# cat jihe.sh

#!/bin/sh

A=$1

B=1

Sum=0

While [$b-le $a]

Do

Let sum= $sum + $b

Let b++

Done

Echo $sum

[[Email protected] 02]# SH jiuhe.sh 100

5050

[Email protected] 02]#

=======================

[email protected] 02]# cat jihe.sh

#!/bin/sh

A=$1

B=1

Sum=0

While [$b-le $a]

Do

sum=$ (($sum + $b))

b=$ (($b + 1))

Done

Echo $sum

[[Email protected] 02]# SH jihe.sh 100

5050

=======================

Highest efficiency with arithmetic progression calculation

[email protected] 02]# cat jihe.sh

#!/bin/sh

A=$1

Sum=0

Sum=$ (($ ($a *$ ((1+ $a))/2))

Echo $sum

[[Email protected] 02]# SH jihe.sh 100

5050

[[Email protected] 02]# sh jihe.sh 1000000

500000500000

[Email protected] 02]#

=========================

######################################

Example 2

Mobile phone recharge 100 yuan, each call 3 yuan, when the balance is less than 0 yuan, the prompt balance is insufficient, please recharge

Implemented with the while statement

[email protected] 02]# cat while.sh

#!/bin/sh

m=100

While [$m-ge 0]

Do

echo "Send message:m = $m"

Let m=m-3

Sleep 2

Done


echo "Money was not enough $m: pleses input Momey!"

[Email protected] 02]#

[Email protected] 02]# sh while.sh

Send message:m = 100

Send message:m = 97

Send message:m = 94

Send message:m = 91

Send message:m = 88

...

...

Send message:m = 13

Send message:m = 10

Send message:m = 7

Send message:m = 4

Send message:m = 1

Money isn't enough-2: pleses input Momey!!

[Email protected] 02]#

######################################

Example 3

Calculates the total number of access bytes for all rows in the log access.log of the Apache day, giving the implementation program.

Tips:

Ways to get the contents of a file:

Method 1

Cat A.log |while Read line

Do

Done

==============

Method 2,

While Read line

Do

Done < A.log

==============

Method 3,

EXEC <a.log

While Read line

Do

Done

==============

[email protected] 02]# cat apache.sh

#!/bin/sh

Sum=0

I=0

While Read line

Do

i=$ (echo $line |awk ' {print $} ')

If expr $i + 1 &>/dev/null

Then

sum=$ (($sum + $i))

Fi

Done < Access.log

Echo $sum

[Email protected] 02]#

###################

Did not find the appropriate example, all I use the following access log, feel free to find a log file to get a number to calculate

[Email protected] 02]# sh apache.sh

2982

[email protected] 02]# cat apache.sh

#!/bin/sh

Sum=0

I=0

While Read line

Do

i=$ (echo $line |awk ' {print $} ' |awk-f ' = ' {print $} ' |cut-c 10)

#echo $i

Expr $i + 1 &>/dev/null

A=$?

If [$a-eq 0]

Then

sum=$ ((sum+i))

Fi

Done < Access.log

Echo $sum

[Email protected] 02]# head-3 Access.log

[172.16.0.183] [18/sep/2015:00:05:03 +0800] [Get/url?a=115020310073&&u=104300025226300150914&b=460036361697955&c=a1000037d69c7c&g=0 &s=0&v=5.2.27.4ctch1&p=0&id=20150917220000&f=0 Http/1.1][200][31][[0]]

[172.16.0.182] [18/sep/2015:00:05:03 +0800] [Get/url?a=115020310073&&u=104300009080897150319&b=460031390615622&c=a000004f85440d&g=0 &s=0&v=5.2.27.4ctch1&p=0&id=20150917220000&f=0 Http/1.1][200][31][[0]]

[172.16.0.184] [18/sep/2015:00:05:03 +0800] [Get/url?a=115020310073&&u=104300024401506150906&b=460036860899028&c=a100004818c4b6&g=0 &s=0&v=5.2.27.4ctch1&p=0&id=20150917220000&f=0 Http/1.1][200][31][[0]]

[Email protected] 02]#


###################

#########################################################################

Example 4. Simply type in a person's name to produce a 4-bit random number, and the first digit of the random number cannot be 0. If it is 0, regenerate a four-bit random number.

[email protected] 03]# cat ran.sh

#!/bin/sh

A=$1

While True

Do

Num= ' echo $RANDOM |cut-c 1-4 '

B= ' echo $num |cut-c 1 '

If [$b-GT 0]

Then

echo "$a =========> $num"

Exit 0

Fi

Done


[Email protected] 03]#

=======================

[Email protected] 03]# sh ran.sh xia

xia=========>3121

[Email protected] 03]# sh ran.sh xia0

xia0=========>7314

[Email protected] 03]# sh ran.sh xia3

xia3=========>1818

#########################################################################

Example 5, Sync Mysqlbinlog every 10 seconds, daemon mode

While True

Do

rsync-az/application/mysql/data/mysql-bin* [Email Protected]::backup--password-file=/etc/rsync.password

Sleep 10

Done


########################


While loop summary

1, while the special feature of the loop is to carry out the daemon, has hoped that the loop does not exit the continuous execution, for frequency less than 1 minutes of the loop out, the other while loop can almost be used for a loop instead of

2, case statements can be replaced with if statements, generally in the system startup script to pass in a small number of fixed-rule strings when used, and other multi-use if instead

3, if, for statements most commonly used, followed by while (daemon), Case (service startup script).


This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1701422

While loop usage

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.