Get the App
SLTechnology News&Howtos  ›  Servers  › 

Combing the basic knowledge of shell script & lt; Nine & gt;:break jumps out of the whole cycle; continue jumps out of this cycle

Shulou Source: shulou.com Published: 2022-06-02 12:00:16 10月02日 Update

The break command allows you to jump out of all loops (terminate the execution of all subsequent loops)

#! / bin/bash

While:

Do

Echo-n "enter a number between 1 and 5:"

Read aNum

Case $aNum in

1 | 2 | 3 | 4 | 5) echo "the number you entered is $aNum!"

*) echo "the number you entered is not between 1 and 5! game over"

Break

Esac

Done

Break instance 2

#! / bin/bash

I, 1

For day in mon tue wed thu fri

Do

Echo "Weeking $((iTunes +)): $day"

If [$I-eq 3]; then

Break

Fi

Done

Execution result

[root@zabbix shell] # sh break.sh

Weeking 1: mon

Weeking 2: tue

The continue command is similar to the break command, except that it does not jump out of all loops, just out of the current loop.

Modify the above example:

#! / bin/bash

While:

Do

Echo-n "enter a number between 1 and 5:"

Read aNum

Case $aNum in

1 | 2 | 3 | 4 | 5) echo "the number you entered is $aNum!"

*) echo "the number you entered is not between 1 and 5!"

Continue

Echo "Game over"

Esac

Done

Contibue instance 2

#! / bin/bash

I, 1

For day in mon tue wed thu fri sat sun

Do

Echo-n "Day $((iTunes +)): $day"

If [$I-eq 7-o $I-eq 8]; then

Echo "(WEEKEND)"

Continue

Fi

Echo "(weekday)"

Done

Execution result

[root@zabbix shell] # sh break.sh

Day 1: mon (weekday)

Day 2: tue (weekday)

Day 3: wed (weekday)

Day 4: thu (weekday)

Day 5: fri (weekday)

Day 6: sat (WEEKEND)

Day 7: sun (WEEKEND)

Tags: Numbers inputs loops between commands examples results examples only differences facets basics knowledge scripts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Shulou Tech Info Apple Xiaomi