Linux Shell Learning: How to call functions in the shell

Source: Internet
Author: User
Tags definition count linux

Speaking of function calls, I believe that we will not be unfamiliar, however, for beginners shell of me, the shell function call Way is a little let me not too accustomed to, I have gone a lot of detours, because the transfer parameters when a very "natural" error, but also let me eat a lot of pain, So summarize the call method for functions in the shell.

The definition of functions in a shell

To facilitate program and management and modularity and to reduce code duplication, functions are indeed 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 the (), and it is not like the C language, where there can be parameters.

Then everyone may be depressed, the function call will more or less always need some parameters, then these parameters how to pass in? In fact, the parameter transfer mode is: fname (no need to pass parameters) or fname agr1 arg2 (need to pass two parameters);

Ii. Examples of custom functions

I don't know what's happening to you, anyway, I felt awkward at first, because in C, for example, I define a function int cmp (int a, int b), then I would use the variables A and B declared in the function header in the function, without defining the parameters in the shell. Then my function needs to use these two parameters, how to do? Here is an example to illustrate the good.

#! /bin/bash  
# Filename:LoopPrint.sh  
      
function Loopprint ()  
{  
    count=0;  
    While [$count-lt $];  
    Do  
    echo $count;  
    Let ++count;  
    Sleep 1;  
    Done return  
    0;  
}  
      
Read-p "Please input the times of print for you want:" N;  
Loopprint $n;

First of all, the function of the program is to enter a number n, and then starting 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 perform the output function.

Note the sentence in Note 1, there is a variable in it, we should remember the call function when the parameters of the way, that is, fname Agr1 arg2, where the $ is the first parameter, and so on, $ is the second argument, $ is the 3rd parameter, $n is to represent 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 parameters passed to the script;

$@: Is the list of all parameters passed to the script, which is extended to "$" "$" "$";

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

$$: is the current process ID number of the script running;

$: is to display the exit status of the last command, 0 indicates that there are no errors, others indicate that there are errors;

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

Iii. Scope Issues

The scope of a function is the same as the function constraint in the C + + language, the definition of a function must appear before the call statement of the function, but there is a point that is not the same as C + + in the scope of the variable, after my experiment, in the comment 1 statement changed to while [$count-lt $n]; It is also possible that the function can use any of the variables that appear in this file, but I recommend using the method in the example above, while [$count-lt $], and do not arbitrarily use variables other than those in the function. Because you don't necessarily know what variables exist outside the function when you call the function, and you don't know what the value is, there is no guarantee that someone else will use your function to pass the name of the variable you use in the function, such as n here, when someone else is using a variable that he or she defines, such as count.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

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.