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

Shell programming-case multi-branch and loop statements (including operation experiments)

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The main points of this chapter:

Case multi-branch

Loop statement

I. the structure of the case statement

Execute different command sequences according to different values of variables

Case variable value in case control instruction in mode 1) start) command sequence 1 starts XX service; Mode 2) "stop) Command sequence 2 stops the XX service;;";. . *) "*) the default command sequence shows the usage of the service script esac esac

Example 1

● keystroke type recognition

Prompt the user for a character

Judge whether the character is a letter, number or other character

Example 2

Prompt the user to enter a grade

Judge the grade of achievement

2. Circular sentence

1. The structure of for statement

Read the values of different variables to execute the same set of commands one by one

For variable name in value list for recipient in email address list do > do command sequence > send email done done

Example 1

Sequential output digits 1-10

Example 2

Add users in batch

The ● user name is stored in the users.txt file, one per line

The initial passwords are all set to 123456

Validate script

Example 3

● checks the host status based on the IP address

The IP address is stored in the ipadds.txt file, one per line

Use the ping command to test the connectivity of each host

2. While sentence structure

Repeatedly test a condition and execute it repeatedly as long as the condition is established

While conditional test operation while failed to guess the correct price do > do command sequence > repeatedly guessed commodity price done done

Example 1

● add users in batch

The user name begins with stu and is numbered in numerical order

Add a total of 20 users, namely stu1, stu2, .stu20

The initial passwords are all set to 123123

Example 2

● guesses the price of goods

Random numbers are obtained by variable RANDOM

Prompt the user to guess and record the number of times, and then exit the loop.

3. The structure of until statement

Repeatedly test a condition and execute it repeatedly as long as the condition is not valid

Until conditional test operation while does not exceed 10do > do command sequence > number increment done done in turn

Example 1

● calculates the sum of 1 to 50

Calculate the sum of 1: 50 by cyclic accumulation

Example 2

● sends an online message for the specified user

If the user is not online (not logged in to the system), try it every 5 seconds until the user logs in to the system and sends a message.

The user name and message are passed to the script through the location parameter

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

Servers

Wechat

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

12
Report