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 the shell function in Linux system

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

Share

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

This article mainly analyzes the relevant knowledge points of how to use the shell function of Linux system, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to use the shell function of the Linux system".

Do you see that function is a high-end command only in mathematics? There is not a function in the Linux system, that is the shell function. Unlike the function in mathematics, the function of the shell function is actually to reduce the amount of code.

Definition of shell* and function * *

\ 1. Linux shell function

Form a set of command sets or statements into an available block called a function.

\ 2. The composition of shell function

Function name: function name, note that the function name should be unique in a script, otherwise it will cause the calling function to be confused.

Function body: a collection of commands within a function to implement a business function.

\ 3. Format of shell function definition

The function function name () # function can be omitted. Note that () does not take any arguments inside.

{

Command 1

Command 2

. . .

}

Examples of how to use a function

1. Function definition and reference example 1: the simplest definition and reference

[liu@WLJY01 myscript] $sh func_1.sh

The definition and reference method of the walkthrough function:

Welcome,now time is Wed Aug 12 16:01:58 CST 2015

Actual environment operation:

Execution result:

two。 Function definition and reference example 2: passing parameters to function exercise

Passing arguments to a function is like using a special variable 2. 0 in a general script. . . Like $9, after the function gets the passed parameters, it is best to reset the variables in the function to save the passed parameters.

Program function: pass a variable value to the function, carry on the related operation to the variable value, and output the result.

[liu@WLJY01 myscript] $sh func_2.sh

The method of passing parameters in the exercise function (pay attention to the receiving method of variables)

The number of parameters that meet the requirements of the function

10: 90: 100

Actual environment operation:

Execution result:

\ 3. The function file and script file are not in the same file, how can I call this function?

Program function: enter the path of a file, and then print out the MD5 summary information of the file.

[liutaostdio@WLJY01 myscript] $cat func_3.sh # function file

[liutaostdio@WLJY01 myscript] $sh func_call.sh # script file

Please enter a file name: test1.sh

The md5 summary of test1.sh is: 5615ac3ce36ec58748a80649c3599d88 test1.sh

Please enter a file name: test2.sh

The md5 summary of test2.sh is: 4446b536dff610ea440663147914c52b test2.sh

Please enter a file name: ttt.sh

The md5 summary of ttt.sh is: 5615ac3ce36ec58748a80649c3599d88 ttt.sh

Note the method of function import:

. Space character file name

Actual environment operation:

Execution result:

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

This is the end of the introduction on "how to use the shell function in the Linux system". More related content can be searched for previous articles, hoping to help you answer questions, please support the website!

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

  • How to use css to realize the Animation effect of Water pattern Diffusion

    Editor to share with you how to use css to achieve watermark diffusion animation effect, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

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

    12
    Report