Linux Shell Custom Function method (definition, return value, variable scope) _linux shell

Source: Internet
Author: User
Tags variable scope

First, define the shell function (define functions)

Grammar:

[Function] funname [()]
{
Action
[Return int;]
}

Description

1, can be defined with function fun () or directly fun (), without any parameters.
2, the parameter returns, may display adds: Return returns, if does not add, will run the result with the last command, as the return value. return followed by value N (0-255

Instance (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.sh 
testfun1.sh:line 3:fsum:command not found 
5,7 
3,2

From the above example we can get a few conclusions:

1, must declare the function before calling the function place, the shell script is run row by line. Does not precompile like any other language. You must declare the function first before using the function.

2, total=$ (fsum 3 2); With this call method, we know clearly that inside the single bracket in the shell, it can be: a command statement. Therefore, we can consider the function in the shell as defining a new command, which is a command, so that each input parameter is separated directly by a space. Once, the command to get the parameters of the method can be passed: $ ... $n get. The $ $ represents the function itself.

3, function return value, only through $? The system variable is obtained, the direct pass =, the obtained is the null value. In fact, we follow the above understanding, know that the function is a command, in the shell to obtain the command return value, all need to pass the $?

Second, function scope, variable range of action

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

#!/bin/sh 
 
echo $ (uname); 
declare num=1000; 
 
Uname () 
{ 
  echo "test!"; 
  ((num++)); 
  return; 
} 
TestVar () 
{local 
  num=10; 
  ((num++)); 
  echo $num; 
 
} 
 
uname; 
echo $? 
echo $num; 
TestVar; 
echo $num; 
         
                
SH testfun2.sh 
Linux 
test! 
1001 
1001

Let's analyze the example above, and we can get the following conclusion:

1, the definition function can be the same as the system command, the Shell Search command, first of all in the current shell file defined in place to find, find direct execution.
2, need to obtain function value: through $?
3. If you need to spread other types of function values, you can define the variable (this is the global variable) before the function call. You can modify it directly inside the function, and then you can read the modified value in the execution function.
4, if you need to define their own variables, you can define in the function: local variable = value, when the variable is an internal variable, its modification does not affect the value of the same variable outside the function.

These are my work on Linux, Shell functions using some experience summary, there is no mention of places, welcome to communicate!

This is supplemented by other netizens:

All scripts so far in this tutorial have been executed from beginning to end. This is good, but you may have noticed that some of the script segments are duplicated.

The shell allows a set of command sets or statements to be formed into a usable block called a shell function.

Functions in the shell are defined in the following format:

Function name () {
Command1
Command2
...
CommandN
[Return value]
}

If you prefer, you can precede the function name with a keyword function, depending on the user.
function functions name () {
Command1
Command2
...
CommandN
[Return value]
}

function returns a value that shows the increment return statement, and if not, the last command runs as the returned value (typically 0, which returns an error code if execution fails). Return followed by a value (0-255).

A function can be placed in the same file as a piece of code, or it can be placed in a separate file that contains only functions. A function does not have to contain many statements or commands, and it can even contain only one echo statement, depending on the consumer.

The following example defines a function and makes a call:

#!/bin/bash
Demofun () {
echo "This is your"
echo "function begin ..."
Hello
echo "function end!" Output:
Function begin ...
This is your the function!
Function end!

The following defines a function with a return statement:

#!/bin/bash
Funwithreturn () {echo "The ' function is ' to get ' the
sum of two numbers ..."
echo-n "Input-A-numb ER: "
read anum
echo-n" Input Another number: "
read Anothernum
echo" The two numbers $aNum and $anot Hernum! "
Return $ (($aNum + $anotherNum))
}
Funwithreturn
echo "The sum of two numbers is $?!" The output is similar to the following: The function is to get the
sum of two numbers
... Input number:25
input Another number:50 the
two numbers are!
The sum of two numbers is 75!

function returns the value after calling the function through $? To obtain.

Note: All functions must be defined before they are used. This means that you must place the function at the beginning of the script until the shell interpreter discovers it for the first time. Call functions use only their function names.

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.