shell function_1

Speaking of function calls, I believe that everyone is familiar with it. However, for me, who is a beginner in Shell, the way of calling functions in Shell is a bit unaccustomed to me, and I have taken a lot of detours myself, because there is a problem when passing parameters. Very "natural" mistakes, but also made me suffer a lot, so I will summarize the calling method of functions in Shell.

First, the definition of functions in Shell
In order to facilitate the program and management and modularization and reduce code duplication, the function is indeed a good thing. There are two ways to define functions in Shell, as follows:
function fname()
{
statements;
}
or
fname()
{
statements;
}
Note that there is no parameter in (), it is not like C language, in () can have parameters.

Then everyone may be depressed. Function calls will more or less always require some parameters, so how should these parameters be passed in? In fact, the parameter passing method is: fname; (no need to pass parameters) or fname agr1 arg2 (two parameters need to be passed);

2. Examples
of In the C language, for example, if I define a function int cmp (int a, int b), then I will use the variables a and b declared in the function header in the function, but no parameters are defined in the Shell, then I will The function needs to use these two parameters, how to do it? Here's an example to illustrate.
[plain] view plaincopyprint?
#! /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, which is to input a number n, and then start from 0 and input a number every 1 second until n-1 is output. First, the program will ask you to input a math, and then call the function to perform the output function.

Pay attention to the sentence in Note 1, there is a variable $1 in it, you should remember the way the parameters are passed when calling the function, that is, fname agr1 arg2, where $1 means the first parameter, and so on, $2 is the second parameter , $3 is the third parameter, $n is the nth parameter.

So $1 is the value of variable n. That said, everyone understands!

To add, it is:
$0: is the name of the script itself;
$#: is the number of parameters passed to the script;
$@: is a list of all parameters passed to the script, which is expanded to "$1" "$2" "$3 "Wait;
$*: Displays all parameters passed to the script as a single string. Unlike position variables, the number of parameters can exceed 9, that is, it is expanded to "$1c$2c$3", where c is the first character of IFS;
$$: is the current process ID number of the script running;
$?: is to display the exit status of the last command, 0 means no error, other means there is an error;

special attention, when passing parameters, (in this example) must be written as LoopPrint $ n; and cannot be written as LoopPrint n. Why? For example, if you input 20, the value of n ($n) is 20. The former means that the value of n, that is, 20, is passed to the function LoopPrint, while the latter means that the character n is passed to the function LoopPrint. This is very different from the function parameter passing in static languages, because the use of variables in the shell does not need to be defined first, so to use variables, let the shell know that it is a variable, and to pass its value, It is to use $n, but not to use n directly, otherwise only n is treated as a character, not a variable.

3. Scope
problem The scope of a function is the same as the role constraints in the C/C++ language. The definition of the function must appear before the calling statement of the function, but one thing that is different from the C/C++ language is the role of variables Domain problem, after my test, it is also feasible to change the statement in Note 1 to while [ $count -lt $n ];, that is, the function can use any variable that appears in this file, but I still recommend using the above example. method, i.e. while [ $count -lt $1 ], and don't use variables other than variables in the function at will, because you don't necessarily know what variables exist outside the function when you call the function or what its value is, There is also no guarantee that others will pass the variable names you use in the function when using your function, such as n here. When others use it, they may pass their own variables, such as Count, etc.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326969160&siteId=291194637