In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the function, function definition and scope in Shell". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to understand the function, function definition and scope in Shell".
1. The definition of function in Shell
Functions are really a good thing in order to facilitate programming and management and modularization and to reduce code duplication. There are two ways to define a function in Shell, as follows:
The code is as follows:
Function fname ()
{
Statements
}
Or
The code is as follows:
Fname ()
{
Statements
}
Note that there are no parameters in (). Unlike the C language, it can have parameters in ().
Then you may be depressed, function calls will always need some parameters, so how to pass these parameters in? In fact, the parameter passing method is: fname; (no need to pass parameters) or fname agr1 arg2 (two parameters need to be passed)
Second, examples of custom functions
I don't know what everyone's situation is, but I felt awkward at the beginning, because in C language, for example, I define a function int cmp (int a, int b), then I will use the variables an and b declared in the function header, but there are no parameters defined in Shell, then my function needs to use these two parameters, what should I do? Let's use an example to illustrate it.
The code is as follows:
#! / bin/bash
# Filename:LoopPrint.sh
Function LoopPrint ()
{
Count=0
While [$count-lt $1]
Do
Echo $count
Let + + count
Sleep 1
Done
Return 0
}
Read-p "Please input the times of print you want:" n
LoopPrint $n
Let's talk about the function of this program first, that is, enter a number n, and then enter a number every 1 second from 0 until you output nmur1. First of all, the program will ask you to enter a math, and then call the function to output the function.
Note that in the sentence in note 1, there is a variable $1. You should remember how to pass the parameter when calling the function, that is, fname agr1 arg2, where $1 represents the first parameter, and so on, $2 is the second parameter, $3 is the third parameter, and $n is the nth parameter.
So $1 is the value of the variable n. Do you understand?
To add, that is:
$0: is the name of the script itself
$#: the number of parameters passed to the script
$@: is a list of all parameters passed to the script, that is, extended to "$1", "$2", "$3", and so on.
$*: displays all parameters passed to the script as a single string. Unlike position variables, there can be more than 9 parameters, that is, extended to "$1c$2c$3", where c is the first character of IFS.
$: the ID number of the current process under which the script is running
$?: displays the exit status of the last command. 0 means there is no error, and others indicate that there is an error.
In particular, when passing parameters, (in this case) must be written as LoopPrint $n; not as LoopPrint n. Why? For example, if you enter 20, the value of n ($n) is 20. The former means passing the value of n, that is, 20, to the function LoopPrint, while the latter means passing the character n to the function LoopPrint. This is very different from passing function parameters in a static language, because the use of variables in Shell does not need to be defined first, so use variables to let Shell know that it is a variable and pass its value with $n, not directly, otherwise n is treated as a character rather than a variable.
III. Scope
The scope of the function is the same as that of the function constraint in the Cstroke + language, and the definition of the function must appear before the calling statement of the function, but there is one difference between the function and the call statement of the function, which is the scope of the variable. After my experiment, the statement in note 1 is changed to while [$count-lt $n]. It is also feasible, that is, the function can use any variable that appears in this file, but I still suggest using the method in the above example, that is, while [$count-lt $1], and do not use variables other than those in the function at will, because you do not necessarily know what variable exists outside the function and what its value is when you call the function. There is no guarantee that others will pass the name of the variable you use in the function when using your function, such as n here, others may pass their own defined variables, such as Count, etc.
At this point, I believe you have a deeper understanding of "how to understand the function, function definition and scope in Shell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.