Ten minutes Easy Learning Series: 2020-3-20_SHELL _ function based learning

Out of consideration of the training section, because the subsequent need to complete a variety of functions with the demo, we first talk about the rough function, in order to avoid the follow-up course, a function not understand, do not know why the wording intended

1. Create a method functions:

#!/bin/bash             #固定shebang写法
IP=192.168.1.1          #定义变量: <name>=<value> 一般变量名都是大写,=左右无空格挨着写 
Add() {                 #函数头             
 	
	echo "赞美太阳~"     #函数体

}
Add                     #调用过程       

The above is a complete no-argument function call, you can paste the contents of the script to run directly in the shell
, unlike other programming languages, without having to call the function (), equivalent to directly write the function name, the function call is completed

There is no function of the location? ?
Guess the top Add if you can call the function below? ?

#!/bin/bash
Add

Add() {
    TEAM=ccb
    local TEAM=bbjyz
    echo $TEAM       
}

Add

The result of pit father can not tell you, that is the same function definitions and variables at the beginning you have to write it all, absolutely not call functions over function! ! ! ! !

Here Insert Picture Description

There parameters: NOTE function xxx (args ...) {} shell language such an approach does not exist, that is only transmitted bit parameter $ 1, $ 2 ...

#!/bin/bash
Add() {
   echo $1
   echo $2
   expr $1 + $2	 # expr函数后方 $1+$2这种方式不对,必须空格 $1(空格)+(空格)$2
}
Add 10 20  

See the results, after I call the function parameter passing, all belong to the internal behavior of the shell script
Here Insert Picture Description
Another point, the reference position within the scope of the function exists, ie calling a function with the orientation parameters can be passed into the function after the function, the script bit parameter is not transmitted to the bit parameters within the function, illustrated, because the function has $ 1 and $ 2, so that I have sent bit parameter in external script, the result can be seen, the internal function BITPAR due scope of the function, and can not act throughout the shell script

In other words, you shell out mass participation, none of my position within the function parameters hair thing!

Here Insert Picture Description

2. Scope of the problem function:

General programming language variables with a different scope, see detailed study:
(A) an arbitrary position in the shell, it does not matter within the function, outside a function, a method usually <key> = <value> defined variables are global variables.
1 defines a function k = v, in the function echo $ k 2 can be taken to a value v.
(b) If the definition of a function with variable afraid of the same name outside the function variables lead to conflict, then you need local keywords.
The function is defined in the form of variables local = the variable can be ensured only within this function.

#!/bin/bash
echo $TEAM              #因为shell是解释型语言,此时变量TEAM是不存在的,所以打印为空
Add() {
    TEAM=ccb            #全局变量TEAM,只有在本行定义后面的行才可调用它
	local TEAM=bbjyz    #局部变量TEAM,与全局同名但是完全不冲突,因为作用域不同
	echo $TEAM          #函数中有同名的全局和局部变量,函数内打印的是局部变量,也就是狭隘原则使用 ===> bbjyz
}
Add                     #调用函数
echo $TEAM              #函数外自然打印出的是全局变量,而不是局部作用域变量 ===> ccb

The results are as follows: how to explain? TEAM come up variable does not exist, so the print is empty; until the global variables inside a function assignment, so the TEAM will have a value within the function of local higher priority than global variables, it also defines TEAM variables of the same name, but the Add function result of the call is local variables, external variables of course, the last functions can not be used inside a function local variables, global variables must be
Here Insert Picture Description
look below for further complemented described above:

#!/bin/bash
echo $TEAM        #空       
Add() {
    TEAM=ccb            
        local TEAM=bbjyz    
        echo $TEAM     # 函数内部局部变量优先级高于全局,所以为bbjyz  
}
Add                 

Add1() {
        echo $TEAM     #函数中定义过一个全局,所以我是全局变量 ccb
}
Add1

echo $TEAM   #ccb

Look at the results:
Here Insert Picture Description

3. Why do you want to use the function:

advantage:

  • Package function, improve code reuse capabilities, simple and easy to read the script;
  • If the average weight is calculated for each line in the department members compared to years ago, how much fat, you can write a function to calculate the difference between the average value provided;
  • Only need to pass the position parameter which department, department how many people, how many average weight, do not write each department a computing function.

Disadvantages:

  • And compiled languages, and can not call a function in position before the definition of the pit father!
    The Python language to illustrate, Golang for the main function defined outside, it does not matter where the location, you can call.
     #!/usr/bin/python
     res = Add(3,1)
     print(res)
     
     def Add(x,y):
         return x+y
     
     res = Add(3,1)
     print(res)
    
    Results also pit father, explanatory language on this virtue, tell you what you did not define what ya call? . . .
    Here Insert Picture Description
    After defining Add, then call, there would be no problem: 0
    Here Insert Picture Description
    Here Insert Picture Description

Here again leads to a knowledge point, above the shell life and let me write a python function? ? I am by what distinguish shell script or python script? ? I said from the first day of Shebang, Shebang, Shebang, what is Shebang? ? ?

Online bunch of long-winded, look interested, I reduced to a few words as follows:

  • shebang is not a comment! Not a comment! Not a comment! !
  • shebang can make different interpreters do different things in the script, some complex logic can easily choose the best implementation language
  • Different shebang can be achieved with a shell script, different languages ​​nesting relationship, this I say tomorrow, need to do some tests
Published 49 original articles · won praise 18 · views 3990

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/104988762