Function call in shell of Linux Shell

Source: Internet
Author: User
Speaking of function calling, I believe everyone will not be unfamiliar with it. However, for me new to shell, the function calling method in shell is a bit difficult for me and I have taken a lot of detours, because a "natural" error occurred while passing parameters, and I suffered a lot of pains, I would like to sum up the method for calling functions in shell. I. The function definition in shell is indeed a good thing to facilitate program and management and modularize and reduce code duplication. There are two methods to define a function in shell: function fname () {statements;} Or fname () {statements;}. Note that there is no parameter in, it does not have parameters in () as in C language. Then everyone may be depressed. Some parameters are always required for function calls. How can these parameters be passed in? In fact, the parameter transmission method is fname; (No parameter is required) or fname AGR1 arg2 (two parameters need to be passed). 2. The example of a user-defined function does not know what the situation is like, at the beginning, I thought it was quite awkward, because in C language, for example, I defined an int CMP (int A, int B) function ), then I will use the variables A and B declared in the function header in the function, but no parameters are defined in shell, so my function needs to use these two parameters, what should I do? The following is an example.
#! /bin/bash# Filename:LoopPrint.shfunction LoopPrint(){count=0;while [ $count -lt $1 ];doecho $count;let ++count;sleep 1;donereturn 0;}read -p "Please input the times of print you want: " n;LoopPrint $n;
Let's talk about the function of this program. It is to input a number N, and then input a number every 1 second from 0 until N-1 is output. First, the program will require you to enter a math and then call the function to output the function. Note that there is a variable $1 in the comment 1. You should remember the parameter transfer method when calling the function, that is, fname AGR1 arg2, $1 indicates the first parameter, and so on. $2 indicates the second parameter, $3 indicates the first parameter, and $ n indicates the nth parameter. Therefore, $1 is the value of Variable N. So everyone understands it!
In addition, $0 indicates the Script Name, $ # indicates the number of parameters passed to the script, and $ @ indicates the list of all parameters passed to the script, expanded to "$1" "$2" "$3", etc. $ *: displays all parameters passed to the script in a single string, which is different from the location variable, up to 9 parameters can be expanded to "$ 1C $ 2C $3", where C is the first character of IFS; $: is the ID of the current process running the script;
$? : The exit status of the last command is displayed. 0 indicates no error. Others indicate errors. Note that when passing parameters (in this example), you must write them as loopprint $ n; it cannot be written as loopprint n. Why? For example, if you enter 20, the value of N ($ N) is 20. The former indicates that the value of N is passed to the loopprint function, the latter means to pass the character n to the loopprint function. This is very different from the function parameter transfer in the static language, because the use of variables in shell does not need to be defined first, so you need to use variables to let Shell know that it is a variable, to pass its value, $ n is used instead of N. Otherwise, n is treated as a character rather than a variable. Iii. Scope problem the function scope is the same as the functional constraints in C/C ++. The function definition must appear before the function call statement, but one thing that is different from C/C ++ is the scope of the variable. After my own test, the statement in Note 1 is changed to while [$ count-lt $ N]; it is also feasible, that is, the function can use any variable in this file, but I suggest using the method in the above example, that is, while [$ count-lt
$1], and do not randomly use variables other than the variables in the function, because you do not necessarily know what variables exists outside the function when you call the function, or what their values are, it cannot be guaranteed that when others use your function, they will pass the variable name you used in the function, such as N, what others may pass during use is their own Defined variables, such as count.
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.