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

Fully understand case statements and functions in Shell programming

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

Share

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

This article mainly introduces in detail the case statements and functions in Shell programming. The sample code in this article is very detailed, which is very suitable for beginners. Interested friends can refer to it.

I. case statement

1. Grammatical structure

two。 Application case

1) the script does not work with different values.

2) choose to do things according to the needs of users

Second, function

1. What is a function?

two。 How do you define a function?

3. How to call a function?

1) current command line call

2) define it in the user's environment variable

3) call in the script

III. Comprehensive cases

1. Task background

two。 Specific requirements

3. Comprehensive analysis

4. Landing realization

Regular expressions

1. What is a regular expression?

two。 What can regularity do?

3. Interpretation of nouns in regularization

4. The first kind of regular expression

1) metacharacters commonly used in rules

2) other common metacharacters in regularization

3) extended regular metacharacters

5. The second kind of regularity

6. Regular expression summary

Regular metacharacter column table

VI. Regular practice homework

1. Document preparation

two。 Specific requirements

VII. Homework after class

Script to build web service

Case statement case statement is a multiple matching statement. If the match is successful, execute the matching command.

1. Syntax structure description: pattern indicates the pattern that needs to be matched case var in defines the variable; var represents the variable name pattern 1) pattern 1; use | split multiple patterns, which is equivalent to the statement that or command1 needs to execute;; two semicolons represent the end of the command pattern 2) command2;; pattern 3) command3 *) default, which does not satisfy the above mode, is executed by default. *) the following statement command4;; esac esac means the end of the case statement. Application case 1) the script does not work with different values.

Specific requirements: execute the corresponding commands when three different parameters, start, stop and restart, are passed into the program

#! / bin/env bashcase $1 in start | S) service apache start & > / dev/null & & echo "apache started successfully"; stop | T) service apache stop & > / dev/null & & echo "apache stopped successfully";; restart | R) service apache restart & > / dev/null & & echo "apache restart completed" *) echo "Please enter what you want to do";; esac2) choose to do things according to the needs of users.

Specific requirements:

The script prompts the user to enter the name of the service to be managed, and then prompts the user to do something about the service, such as startup, shutdown, etc.

#! / bin/env bashread-p "Please enter the name of the service you want to manage (vsftpd):" servicecase $service in vsftpd | ftp) read-p "Please select what you need to do (restart | stop):" action case $action in stop | S) service vsftpd stop & > / dev/null & & echo "the $serivce service has stopped successfully" Start) service vsftpd start & > / dev/null & & echo "the $serivce service has started successfully";; esac;; httpd | apache) echo "apache hello world";; *) echo "Please enter the name of the service you want to manage (vsftpd)";; esac

# ㈢ menu prompts for users to choose what they need to do

Specific requirements:

Simulate a multitasking maintenance interface; when executing the program, first display the general menu, and then make the selection and do the corresponding maintenance and monitoring operations.

* Please select * h display command to help f display disk partition d display disk mount m view memory u view system load Q exit program *

Train of thought:

The menu is printed and interactively allows the user to enter the operation number, and then deal with it accordingly.

Landing to achieve:

Menu print (explode action) #! / bin/env bashcat

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