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

How to use Shell script case statements

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Shell script case statements". In daily operation, I believe many people have doubts about how to use Shell script case statements. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Shell script case statements". Next, please follow the editor to study!

The Shell case statement is a multi-select statement. You can use the case statement to match a value with a pattern, and if the match is successful, execute the matching command. The format of the case statement is as follows:

The code is as follows:

Case value in

Mode 1)

Command1

Command2

...

CommandN

Mode 2)

Command1

Command2

...

CommandN

Esac

Case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be a variable or a constant. After the match discovers that the value matches a certain pattern, all commands begin to execute until;;.

The value detects each pattern that matches. Once the pattern matches, the corresponding command to match the pattern is executed and the other patterns are not continued. If there is no matching pattern, use the asterisk * to capture the value, and then execute the following command.

The following script prompts for 1 to 4 to match each pattern:

The code is as follows:

Echo 'Input a number between 1 to 4'

Echo 'Your number is:\ c'

Read aNum

Case $aNum in

1) echo 'You select 1'

2) echo 'You select 2'

3) echo 'You select 3'

4) echo 'You select 4'

*) echo 'You do not select a number between 1 to 4'

Esac

If you enter different content, you will have different results, such as:

Input a number between 1 to 4

Your number is:3

You select 3

At this point, the study on "how to use Shell script case statements" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report