Loop Control statement:
Continue: End this cycle prematurely, and go directly to the next round of cycle judgment;
While CONDITION1; Do
CMD1
...
if CONDITION2; Then
Continue
Fi
Cmdn
...
Done
Example: The sum of all even numbers within 100;
#!/bin/bash
#
declare -i evensum=0
declare -i i=0
while [ $i -le 100 ]; do
let i++
if [ $[$i%2] -eq 1 ]; then
continue
fi
let evensum+=$i
done
echo "Even sum: $evensum"
Break: Jump out of circulation early
While CONDITION1; Do
CMD1
...
if CONDITION2; Then
Break
Fi
Done
To create a dead loop:
While true; Do
Loop body
Done
Exit Method:
When a test condition is satisfied, let the loop body execute the break command;
Example: The sum of the odd numbers within 100
#!/bin/bash
#
declare -i oddsum=0
declare -i i=1
while true; do
let oddsum+=$i
let i+=2
if [ $i -gt 100 ]; then
break
fi
done
Sleep command:
-Delay for a specified amount of time
Sleep Number
Exercise: Every 3 seconds to the system to obtain the user's logged on user information, where if the Logstash user logged in the system, the log is logged, and exit;
#! / bin / bash
#
while true; do
if who | grep "^ logstash \>" &> / dev / null; then
break
fi
sleep 3
done
echo "$ (date +"% F% T ") logstash logged on" >> /tmp/users.log
Use untill
#! / bin / bash
#
until who | grep "^ logstash \>" &> / dev / null; do
sleep 3
done
echo "$ (date +"% F% T ") logstash logged on" >> /tmp/users.log
This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1916025
Shell script Programming loop control statement (CONTINUE/BREAK/SLEEP)