Shell Program Design-Functions

Source: Internet
Author: User
Shell programming-functions-general Linux technology-Linux programming and kernel information. For more information, see the following. Function is indispensable for developing a large shell program. It can reuse code with high degree, simplify development, modularization, and strong team cooperation.
Function Definition Format:
Functionname ()
{
Statement Group
}
Function usage:
1. Before calling a function, you must define the function.
2. function parameter transfer: functionname para1 para2 ....
3. Read parameters in the function: $ * $ @ $ #$1 $2 $3 $4 $5...
$0 is not included. The $0 parameter can be referenced inside the function. Therefore, the program parameters cannot be referenced within the function. After the function is executed, these values are restored.
4. function return value: the return command may be used to return the numeric value. To return the string value, you can store the string in a global variable, which can be used by the outside world after the function is completed; if the return command is not used, the return value of the function is the exit status code of the Last Command executed in the function.
5. Use variables: the variables declared in the function are global variables by default, and the variables declared using the local keyword are local variables (such as local var = "var "). If the local variable has the same name as the global variable, the local variable inside the function overwrites the global variable.
6. Obtain the return value. After the function is executed, the return value of the function is stored in $? To obtain the return value of the function.
#! /Bin/sh
# This is a example for testing the return value of function
Fun ()
{
Return 10
}
Fun
Echo $?
Exit 0
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.