shell - (seven) Shell function

Shell is a function of the nature of some script code can be reused, this code is written well in advance and put in the specified location, you can use the direct retrieval.


Shell Functions and C ++ , the Java , Python , C #  is similar to other programming languages and other functions, but some differences in syntax details.

Shell function syntax is defined as follows:

function name() {
    statements
    [return value]
}

A description of each section:

  • functionShell keywords are specifically added to function;
  • nameIs the name of the function;
  • statementsIs the code for the function to be executed, that is, a set of statements;
  • return valueIt represents the return value of the function, in which the return is Shell keyword, specifically used in the function returns a value; this portion can write you can not write.


The { }portion enclosed body called a function, a function is called, the code is actually executed in the function body.

A function definition simplified wording

You can not write function key if you find it troublesome, the function definition:

name() {
    statements
    [return value]
}

If you write a function keyword, you can omit the parentheses after the function name:

function name {
    statements
    [return value]
}

Recommended standard wording, this can be done "to see to know the name meaning" one can understand.

Second, the function call

Can pass parameters to it when calling the Shell function, you can not pass. If you do not pass parameters given directly to the function name:

name

If the parameter passed, then separated by spaces between a plurality of parameters:

name param1 param2 param3

Whatever the form, the function does not need parentheses after his name.

And other programming languages are different, not indicate Shell function defining parameters, the parameters can be passed Shique call, and passing it what parameters any parameters it received.

Shell is not limited by the order to define and call, you can define the front on the call, you can turn, will be defined later in the call.

Example:

1) the definition of a function, the output address Shell tutorial:

! # / bin / bash 
# function definition 
function url () {
     echo  " http://c.biancheng.net/shell/ " 
} 
# function call 
url 


operating results: 
HTTP: // c.biancheng.net/shell/
 
= ================= 
you can call in front of the definition of

2) definition of a function, and calculate all parameters:

#! / bin / the bash
 function GetSum () { 
    local SUM = 0 
    for n- in $ @
     do 
         (( SUM + = n-))
     DONE 
    return $ SUM 
} 
GetSum 10  20 is  55  15   # calling function and pass parameters
 echo $?

The result:
100

$@All parameter indicates the function, $?represents the exit status function (return value).

Three, Shell function parameters

And most of the programming C ++, C #, Python, etc. in different languages, the Shell function parameters can not be specified in the definitions, but you can pass parameters in Shique call.

Shell parameters are a function of the position parameter, within the function may be used $nto receive, e.g., $ 1 represents the first parameter, $ 2 represents the second parameter, and so on.

 

 In addition $n, there are three other important variables:

  • $#You can obtain the number of parameters passed;
  • $@Or $*you can get all the parameters disposable

(1) Example of function parameters:

#! / bin / the bash 

funWithParam () { 
    echo  " first parameter. 1 $! " 
    echo  " The second parameter is 2 $! " 
    echo  " 10th parameter of 10 $! " 
    echo  " 10th parameter is $ 10 { }! " 
    echo  " eleventh parameter. 11 {} $! " 
    echo  " number of parameters for # $! " 
    echo  " string output for all parameters * $! " 
} 
funWithParam . 1  2  . 3  . 4  . 5  . 6  . 7  . 8  . 9  34 is  73 is

Output:

The first parameter is 1 ! 
Second argument is 2 ! 
10th parameter is 10 ! 
10th parameter is 34 ! 
Eleventh parameter 73 ! 
Total number of parameters . 11 a! 
A string of output parameters for all 1  2  . 3  . 4  . 5  . 6  . 7  . 8  . 9  34 is  73 is !

Note that, $ 10 10th parameter can not be obtained, obtain parameters required tenth {10} $. When n> = 10, requires the use of $ {n} to obtain the parameters.

 

(2) Example 2: @ $ to traverse function parameters.

Definition of a function, and calculate all parameters:

#! / bin / the bash
 function GetSum () { 
    local SUM = 0 
    for n- in $ @
     do 
         (( SUM + = n-))
     DONE 
    echo $ SUM 
    return 0 
} 
# calling a function and passing parameters, the final results will be assigned to a variable 
Total = $ (GetSum 10  20 is  55  15 )
 echo $ Total 
# variable may be omitted 
echo $ (GetSum 10  20 is  55  15 )

The result:
100
100

( 3) other special characters to process parameters:

Parameter Handling Explanation
$# The number of arguments passed to the script or function
$* Show all parameters passed to the script to a single string
$$ The script runs the current process ID number
$! Finally, a process running in the background ID number
$@ $ * The same, but the use of quotation marks, and returns each parameter in quotation marks.
$- Shell used to display the current option, and set the same command function.
$? Displays exit status of the last command. 0 means no error, any other value indicates an error.

Four, return value return

1. Have return

(1) ¥? Gets return return value

#! / bin / bash
 function demoFun1 () {
     echo  " This is my first shell function! " 
    return ` expr  1 + 1 ` 
} 

demoFun1 
echo $?
This is my first shell function!
 2

(2) $? Only responsible for its previous instruction, once the function returns the return value is not immediately save the parameter, then its value will not be returned through  $? Get

#! / bin / bash 

function demoFun1 () {
     echo  " This is my first shell function! " 
    return ` expr  1 + 1 ` 
} 

demoFun1 
echo $? # after the call function to get the return value immediately, you can get the return value of
 echo $? # after the function call, the above has been executed other orders, get less than the return value of the function call, but the results obtained on a command (on a single command echo $ 0 for? implementation of the results)

Results of the:

This is my first shell function!
 2 
0

2. No return

If the shell does not return, the default retur 0

shell language 0 represents true, a value other than 0 for false.

3. The results of the implementation of functions and commands that can be used as a conditional statement.

1. Print return results

Example 1:

! # / bin / bash 

echo  " ! the Hello World " | grep - E the Hello
 echo $? 


operating results: 
the Hello World !
 0

Example 2:

echo  " ! the Hello World " | grep - E Bye
 echo ? $ 

result: 
1

grep is to find matching content from a given string command. First see if the contents match is found, the matching portion of the print and return value $ get? 0, if not found, the return value is $? 1.

2. When the return value is determined if the condition

#!/bin/bash

if echo "Hello World !" | grep -e Hello
then
    echo true
else
    echo false
fi

if echo "Hello World !" | grep -e Bye
then
    echo true
else
    echo false
fi


运行结果:
Hello World !
true
false

grep command, respectively, these two performed as a conditional statement to determine if, come the return value of the $? is 0, that is successfully implemented, the conditional statement is true, when the return value is $? 1, that failed to, conditional statement is false.

========

#!/bin/bash

function demoFun1(){
    return 0
}

function demoFun2(){
    return 12
}

if demoFun1
then
    echo true
else
    echo false
fi

if demoFun2
then
    echo ture
else
    echo false
fi

As a return value of the function test in which demoFun1 return value is 0, demoFun2 a return value, and selects any different integers 0, 12 here.

As a function of the if conditional statement to the determination, the return value is 0, is still true stars, and returns the value 0 if not, it is determined that the conditional statement are false.

Guess you like

Origin www.cnblogs.com/zhzhlong/p/12549531.html