Shell programming continue break function return

Source: Internet
Author: User

Jump out of loops and function return values

one, exit cycle

In shell programming, there are usually two ways to exit a loop:

Contine: Is the end of the execution of the loop, the default is Contine 1, when contine 3 o'clock, then the 3rd layer of the loop;

Break: is to jump out of this loop, the default is break 1, when break 2 o'clock, it means to exit the second layer of the loop, into the 3rd layer loop;

So in the loop: contine n = Break n-1

[[email protected] ~/test] #vim for.sh

#!/bin/bash

For i in {1..3};d o

For j in {101..103};d o

For k in {1001..1003};d o

If [$K-eq 1002];than #当执行玩第一轮i =1,j=101,k=1001, Output 1001, enter i=1,j=101,k=10

Contine 3 02 O'Clock Exit back, i=2,k=101,k=1001, Output 1001, enter i=2,k=101,k=

Fi 1002 When exiting back, i=3, in turn, is displayed.

Echo $K

Done

Done

Done

[[email protected] ~/test] #bash for.sh
1001
1001
1001

——————————————————————————————————————————————————

[[email protected] ~/test] #vim for.sh

#!/bin/bash

For i in {1..3};d o

For j in {101..103};d o

For k in {1001..1003};d o

If [$K-eq 1002];than #当执行玩第一轮i =1,j=101,k=1001, Output 1001, enter i=1,j=101,k=10

Break 2 02 O'Clock Exit back, i=2,k=101,k=1001, Output 1001, enter i=2,k=101,k=

Fi 1002 When exiting back, i=3, in turn, is displayed.

Echo $K

Done

Done

Done

[[email protected] ~/test] #bash for.sh
1001
1001
1001

Second, function return value

When the function encounters a return for the first time, it jumps out of the function, similar to the contine in the For loop;

[[email protected] ~/test] #vim return.sh

#!/bin/bash

Ping () {
echo "Ping 127.0.0.1"
PING-C1-W1 127.0.0.1 &>/dev/null && return echo " Exited no "
echo "Ping 127.0.0.2"
PING-C1-W1 127.0.0.2 &>/dev/null && return 20
echo "Ping 127.0.0.3"
PING-C1-W1 127.0.0.3 &>/dev/null &&return 30
}

Ping

[[email protected] ~/test] #bash return.sh

Ping 127.0.0.1

[[email protected] ~/test] #echo $?
10


This article is from the "11831715" blog, please be sure to keep this source http://11841715.blog.51cto.com/11831715/1960270

Shell programming continue break function return

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.