shell script (function definitions and parameters of the call)

                                        Definitions and parameters of the function call

Function is a specific function completion code fragment, defined function codes may be used in a modular shell, to facilitate call, the function must be defined before they can be used.

First, define the function

method one:

Function name() {

        Function code function to be implemented

}


Method Two:

function function name {

        Function code function to be implemented

}


Second, the call parameters

Variable function name

Function name variable value reassignments

Function name position variable $ 1 $ 2 $ 3 .....

The following are some examples of a function parameter passing

The transfer function of the location variable

! # / bin / bash 
position ######## transfer function parameters 
if [$ # -ne 3]; then # If the script then the parameter is not equal three, then print script usage and exit 
        echo " Usage:: `the basename $ 0` PARl PAR2 PAR3"     
        Exit 
Fi 
Fun () { 
        echo "$ [$ 1 * $ 2 * $ 3]" 
        # where $ 1 $ 2 $ 3 is the location parameter function 
} 
Result = `Fun $ 1 $ 2 $ 3` 
# and here $ 1 $ 2 $ 3 script is acceptable position parameter and location parameter function fun script is transmitted to a receiving function 
echo "$ result"


Numeric array variable in the function call

################# 
# define the script array, which has a value 1-5, then arry defines a function, the role of the received value of num external parameters, and each multiply the value with the sum value 
num = (1 2 3 4 5 ) # define an array NUM 
sum 1 = sum defined initial value # 1 
# defined function array 
ARRY () { 
        for I in "$ @" # "$ @" $ * for all parameters also represent all parameters, no need to add "" 
        do 
        the let $ SUM = * i by multiplying the value read #sum $ i, and then assigned to their 
        DONE 
} 
ARRY $ {NUM [@]} # execute the function array, reads the value of $ num. 
echo $ SUM


#! / bin / the bash 
# script above and the action of the same script, the script is executed with the numerical parameters, loop for all numbers, multiplied by one to sum2 
sum2 =. 1 
for J * in $ 
do 
        the let sum2 * $ J = 
DONE 
echo $ sum2

       

Examples The following examples of how to get the definition of the variable parameter and re-assigned to the variable function

local variable names in order not to conflict with other variables, only take effect in the function, does not affect the overall situation

#! / bin / the bash 
############## 
# reassigned to receive variables 
# pass parameters to the function, and recalculates the assignment 
############## ## 
NUM = (2. 3. 1) assigned array # NUM 
#-defined function array 
array () { 
        local newNum = ($ *) # local variables defined function newNum 
        local local variables defined function I # I 
        for ((I = 0; I <$ #; i ++)) # $ # $ for all parameters received Similarly * this line is the starting value of $ i 0, as an index to the following new index value <$ # 
        do 
        Product [$ i] = $ [$ {newnum [$ i] } * 5] # newNum traverse the array each time a value multiplied by five, re-assigned to new new 
        DONE 
} 
array $ {NUM [@]} # call the function array 
echo $ {Product [@] } # output value of $ num array


Small extension, function return values

! # / bin / the bash 
### 
function return value ## funtion return 
############ 
Fun () { 
        Read -p "Enter Number:" NUM 
        the let $ SUM = 2 * NUM 
        # ? return $ value 
        return $ [2 * $ NUM] 
} 
Fun 
#shell return value can not exceed 255, no matter what numbers you enter, the value of the above operations are returned within 255 
echo "fun return value:? $ "



Guess you like

Origin blog.51cto.com/13760226/2412958