Shell script Programming loop control statement (CONTINUE/BREAK/SLEEP)

Source: Internet
Author: User
Tags logstash



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)


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.