Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux? shell statements while and until and case make

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Introduction

Usually there are a lot of repetitive work in the work, and it is impossible to count how many times the loop needs to be executed. At this time, you need while or until loop statements to help you solve some tedious and repetitive work. A case statement matches a value or pattern, and if the match is successful, executes the matching command. We usually use this multi-branch selection structure in scripts. This chapter is a simple introduction to the use of these three statements.

Second, shell loop statements

1. while loop statement

The most common type of statement that enters a loop when a condition is met. When the condition is met, while repeatedly executes a set of statements until the condition is not met.

Grammar:

PS: [For judgment conditions, please check the shell's conditional judgment statement related knowledge]

While loop execution flow:

The first is to judge the condition. If the condition is true, enter the loop and execute the statement in the while loop body (the statement between do and done, thus completing a loop).

Each time when the execution to done will re-determine whether the condition is met, if met, enter the next loop, continue to execute the statement between do and done, if not met, end the whole while loop, execute the other Shell code after done or exit directly.

Example 1: Mass creation of users

Run Results:

Example 2: Calculating the sum of 1 and 1000

Implementation results:

You can see that the application of the while statement is just a simple loop statement. The text just gives a simple example to show the while statement. To achieve complex work, you still need to be familiar with conditional judgment statements in order to write scripts better.

2. Until loop statement

And while to achieve the same function, but the process is different, is a condition does not meet the execution of the loop statement, until the condition is met to exit the loop.

Grammar:

The execution process of the until loop:

First, the condition is judged. If the condition is not satisfied, enter the loop and execute the statement in the until loop body (it is a statement between do and done, which may be multiple statements, so that a loop is completed).

Each time the execution to done will re-determine whether the condition is not satisfied, if not satisfied, enter the next loop, continue to execute the statement between do and done, if satisfied, end the entire until loop, execute the other Shell code after done or exit directly.

Example 1: Mass creation of users (compare with while)

III. Shell branch structure statement

1. Branch structure: case statement (performs different operations according to different values of variables) Usually case structure is used together with shell function to write startup script for source service. Case statements and if... elif... else statement, the same type is a multi-branch conditional statement.

2. Grammar:

Workflow for 3.case statements:

The case statement takes the value in the variable (that is, the value you enter on the command line) and matches it with the value of each branch in the body of the statement. If the values match, execute the program corresponding to the branch; if the values do not match, match the next value in turn; if all the values do not match, execute "*)","*" represents the program in all other values, generally indicating which variables can be filled in.

4. careful

(1) Case statements begin with "case" and end with "esac."

(2) End each branch program with ";;"(double semicolon) to indicate the end of the program segment.

5. example

(1) iptables in Ubuntu system has no startup script, we can make a simple startup and shutdown script through case statement. Define the Iptables configuration storage location/etc/iptables.rule [I made a basic preset]

The results are as follows:

Is it more convenient to set up the iptables startup script in this way? You only need to modify the configuration file and restart the iptables script to take effect. In accordance with this example, you can make some source package procedures into a startup script, hurry to try it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report