Linux shell custom functions (definition, return value, variable scope) Introduction

Source: Internet
Author: User
Tags define function variable scope

http://www.jb51.net/article/33899.htmLinux shell custom functions (definition, return value, variable scope) Introduction

The Linux shell can be user-defined functions and can be called casually in shell scripts. Here's how to define it, and what to call attention to.

I. Defining shell functions (define function)

Grammar:

[Function] funname [()]

{

Action

[Return int;]

}

Description

    • 1, can be with function fun () definition, you can also directly fun () definition, without any parameters. function and () take at least one.
    • 2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. Return followed by a value of N (0-255

Example (testfun1.sh):

#!/bin/sh fsum 3 2; function Fsum () {   echo $1,$2;   Return $ (($1+$2)); } fsum 5 7; total=$ (fsum 3 2); Echo $total, $?; SH testfun1.shtestfun1.sh:line 3:fsum:command not found5,73,215

From the above example we can get a few conclusions:

    • 1. The function must be declared before the function is called, and the shell script runs on a line-by-row basis. does not pre-compile like any other language. You must declare the function first before using the function.
    • 2, total=$ (fsum 3 2); With this method of invocation, it is clear that in the shell, inside the parentheses, it can be: a command statement. Therefore, we can consider a function in the shell as defining a new command, which is a command, so the individual input parameters are separated directly by a space. Once, the command inside the parameter method can be obtained by: $ ... $n. $ A represents the function itself.
    • 3, function return value, only through $? System variables are obtained, directly through =, to obtain a null value. In fact, we follow the above understanding that the function is a command, in the shell to obtain the command return value, all need to get through the $?
Second, function scope, variable action range

Let's look at an example (testfun2.sh):

#!/bin/shecho $ (uname);d eclare num=1000;uname () {  echo "test!";  ((num++));  return 100;} TestVar () {  local num=10;  ((num++));  echo $num;} Uname;echo $?echo $num; testvar;echo $num; SH testfun2.shlinuxtest!1001001111001

Let's analyze the above example together to get the following conclusion:

    • 1, the definition function can be the same as the system command, the Shell Search command, the first will be found in the current shell file defined location, find the direct execution.
    • 2, need to get the function value: through $? get
    • 3. If you need to pass out other types of function values, you can define the variables (this is the global variable) before the function call. It can be modified directly inside the function, and then the modified value can be read out when the function is executed.
    • 4, if you need to define your own variables, you can define in the function: local variable = value, then the variable is an internal variable, its modification, will not affect the value of the same variable outside the function.

These, is I in the work, to the Linux, the Shell function uses some experience summary, has not mentioned the place, welcome the exchange!

Linux shell custom functions (definition, return value, variable scope) Introduction

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.