function calls in the shell of the Linux shell

Source: Internet
Author: User

Talk about the function call, I believe that everyone will not be unfamiliar, but for the beginner shell of me, Shell function call Way is a bit let me not very accustomed to, I also go a lot of detours, because the transfer of parameters when a very "natural" error, but also let me eat a lot of suffering, So summarize the method of calling functions in the shell.


I. Definition of functions in the shell

To facilitate program and management and modularity, and to reduce duplication of code, functions are a good thing. There are two ways to define a function in the shell, as follows:

function fname ()

{

statements;

}

Or

FName ()

{

statements;

}

Note that there is no parameter in (), and it is not like C, it can have parameters in ().


Then everyone may be depressed, function calls more or less will always need some parameters, then how to pass these parameters in? In fact, the parameters are passed: fname; (no need to pass parameters) or fname agr1 arg2 (two parameters required);


Ii. Examples of custom functions

I don't know about everyone, but I felt awkward at first, because in C, for example, I define a function int cmp (int a, int b), then I use the variables A and B declared in the function header in the function, but there are no parameters defined in the shell. Then my function needs to use these two parameters, how to do it well? Let's use an example to illustrate this.

  1. #! /bin/bash

  2. # Filename:LoopPrint.sh

  3. function Loopprint ()

  4. {

  5. count=0;

  6. While [$count-LT];

  7. Do

  8. Echo $count;

  9. Let ++count;

  10. Sleep 1;

  11. Done

  12. return 0;

  13. }

  14. Read-p "Please input the times of print you want:" N;

  15. Loopprint $n;


First of all, the function of this program, is to enter a number n, and then start from 0 every 1 seconds to enter a number until the output n-1. First, the program will ask you to enter a math and then call the function to output the function.


Note that the sentence in note 1, there is a variable, you should remember to call the function when the parameters of the way, that is, fname Agr1 arg2, here is the first parameter, and so on, the second argument, and so on, $ $ is the 3rd argument, $n is the nth parameter.


So $ $ is the value of the variable N. Let's just say that you understand!


To add , it is:

$: Is the name of the script itself;

$#: Is the number of arguments passed to the script;

[Email protected]: is a list of all parameters passed to the script, which is extended to "$" "$" "$" and so on;

$*: is a single string display all the parameters passed to the script, and the location variable, the parameters can be more than 9, that is, expanded to "$1c$2c$3", where C is the first character of the IFS;

$$: is the current process ID number for the script to run;
$?: Displays the exit status of the last command, 0 means no error, and the other indicates an error;


In particular, it is important to note that when passing parameters (in this case) it must be written as Loopprint $n and not loopprint N. Why? For example, if you enter 20, the value of N ($n) is 20, which means that the value of N, or 20, is passed to the function loopprint, while the latter indicates that the character n is passed to the function Loopprint. This is very different from the function parameter passing in the static language, because the use of variables in the shell does not need to be defined first, so the use of variables, let the shell know that it is a variable, and to pass its value, is to use $n, and not directly with N, otherwise only n as a character to deal with, Instead of a variable.


Three, scope problems

The scope of the function is the same as the role of the C + + language, the definition of the function must appear before the function of the call statement, but there is a little different from C + + is the scope of the variable, after my experiment, the statement in note 1 is changed to while [$count-lt $ n ]; it is also possible that the function can use any variable that appears in this file, but I still recommend using the method in the example above, that is, while [$count-lt], and do not arbitrarily use variables outside of the variables in the function, Because you do not necessarily know what variables exist outside of the function when you call the function, do not know what its value is, nor do you guarantee that someone else will pass the variable name you used in the function when using your function, such as N, where others may pass in their own defined variables, such as count, etc.

function calls in the shell of the Linux shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.