Whileconditional Sentence
Grammar:
Condition to meet always done
While condition
Do
instruction
Done
==========
Conditional satisfaction exit (rare)
Until conditions
Do
instruction
Done
Script instance: Daemon monitoring
Sleep: Print per second
Usleep: microsecond printing in microseconds (one out of 10,000 seconds);
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: 2- second print ( dead loop ) Daemon
#Version: version V1.1
#Update: 2016-11-22 15:04:12
While True
Do
# View System Performance
Uptim
print #2 seconds , and save to Uptim.log
Sleep 2 >uptim.log
Done
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: microsecond seconds Print ( dead loop ) Daemon
#Version: version V1.1
#Update: 2016-11-22 15:04:12
While True
Do
# View System Performance
Uptim
print #2 seconds , and save to Uptim.log
Usleep >uptim.log
Done
Script instance:1-100sum
Not suitable for large data large data use formula calculation
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: 1-100 sum
#Version: version V1.1
#Update: 2016-11-22 15:04:12
# define $i values
I=1
# define $sum values
Sum=0
# Judge $i <=100
while ((i<=100))
Do
((Sum=sum+i))
((i++))
Done
# Direct Display value:5050
#echo $sum
# Show Results:sum=5050
#echo "Sum= $sum"
# Beautiful display totalsum is:5050
[-N "$sum"] && printf "totalsum is: $sum \ n"
Calculate ※※※※※ directly using mathematical formulas
[email protected] ~]$ cat gongshi.sh
#!/bin/sh
i=100
echo $ ((i* (i+1)/2))
---------
[email protected] ~]$ cat gongshi-2.sh
#!/bin/sh
i=100
((sum=i* (i+1)/2))
Echo $sum
---------
[[email protected] ~]$ echo $ ((100* (100+1)/2))
5050
Script instance: Lifting display Value
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: display in ascending order
#Version: version V1.1
#Update: 2016-11-22 15:04:12
I=1
Sum=0
while ((i<=100))
Do
((sum=i))
((i++))
Done
#echo $sum
#echo "Sum= $sum"
[-N "$sum"] && printf "totalsum is: $sum \ n"
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: Descending display
#Version: version V1.1
#Update: 2016-11-22 15:04:12
I=1
Sum=0
while ((i<=100))
Do
((sum=i))
((i--))
Done
#echo $sum
#echo "Sum= $sum"
[-N "$sum"] && printf "totalsum is: $sum \ n"
Script instance: web site load Balancing test
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: site Test is linked once
#Version: version V1.1.1
#Update: 2016-11-22 15:04:12
While Trie
Do
Curl-i-S Http://1.1.1.com/|head-l
Sleep 10
Done
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: site test 2 Second link once, have the result of judgment
#Version: version V1.1.2
#Update: 2016-11-22 15:04:12
While True
Do
Ststus= ' Curl-i-S--connect-timeout $ |head-l|cut-d ""-F 2 '
If ["$status" = "$"]; then
echo "This URL is good"
Else
echo "This URL was bad"
Fi
Sleep 2
Done
#!/bin/bash
#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"
#Author: Jorbabe
#Mail: [Email protected]
#Function: site test 2 Second link once, have the result of judgment
#Version: version V1.1.3
#Update: 2016-11-22 15:04:12
# Call system Functions
. /etc/init.d/functions
While True
Do
Ststus= ' Curl-i-S--connect-timeout $ |head-l|cut-d ""-F 2 '
If ["$status" = "$"]; then
echo "This URL is good" /bin/true
Else
echo "This URL was bad" /bin/false
Fi
Sleep 2
Done
Shell script Common script: while