Shell Note 8

Source: Internet
Author: User

  1. A function is the name of a command or a set of commands.

  2. Functions can make the program modular, thus improving efficiency.

  3. The shell stores functions in memory rather than in disk files, so accessing the function is much faster than accessing the script.

  4. The shell pre-process the function so that it starts faster.

  5. Before you can use a function, you must first create the function in the program.

  6. Creating a function is also called defining a declaration of a function or function.

  7. [function] Function name {command table [return]}
  8. which

    (1) The keyword function means defining a function, which can be omitted, followed by the function name. (You can add a pair of empty parentheses after the function name)

    (2) Return in the function returns the exit state value of the last command in the function or the given parameter value.

    (3) If you use the Exit command in a function, you can exit the entire script. If the function exits, it returns to the place where the function was called in the script.

    (4) The break statement can be used to interrupt the execution of the function.

    (5) Use internal command declare-f to display a defined list of functions. If only the name of the function is displayed, you can use the command declare-f.

    (6) You can use the command export-f to output a function to the shell

    (7) may use command unset-f to remove functions from shell memory

  9. function Definition Location : Can be placed in the ~/.bash_profile file, or in a script that uses the function, or can be placed directly on the command line.

  10. Function deletion: You can delete the deletion using the unset command, and the shell will no longer hold these functions once the user logs off.

  11. function invocation , involving function execution, function parameter passing, function return value, load function, and function deletion. Before a function is executed, it must be defined first.

  12. Execute function

  13. When the shell executes an alias, function, internal command, or a disk-based executable, it always executes the alias, then the function, the internal command, and finally the executing program.

  14. Execute functions, simply enter the function name directly.

  15. Example

  16. 1 #! /bin/bash 2 #filename a.sh 3 function Show () 4 {5 Date 6 echo "The LogName:" 7 echo $LOGNAME 8} 9 Show
  17. Functions created from the command line are automatically deleted when the user exits the login.

  18. function parameter passing

  19. Function parameters can be passed by positional variables.

  20. In the command line that invokes the function, the position of the parameter is defined as follows

    $ function Name parameter 1 parameter 2 parameter 3 ...

  21. 1 #!  /bin/bash 2 #filename a.sh 3 function Show () 4 {5 echo $a $b $b $c $d 6 echo $ $4 7} 8 a=111 9 b=222 Ten c=333 d=444 echo "function Begin" show a b c D echo "function End"
  22. # bash A.shfunction Begin111 222 222 333 444a b C dfunction End
  23. 1 #! /bin/bash 2 #filename a.sh 3 cube () 4 {5 item= ' expr $ \* $ \* $ ' 6} 7 8 echo "Please enter a integer:" 9 Read N i=1 result=0 item= ($i-le $N) do $i result= ' expr $RESULT + $ITEM ' i=$ ( $i + 1))-Done echo "compute 1 ... $N de Li Fang He is: $RESULT" NOTE: Do not add spaces at will, after item, = Before and after
  24. return value of the function

  25. The keyword return in a function can be placed anywhere in the body of the function, and is typically used to return certain values.

  26. After the program executes to return, the function stops executing and returns to the calling line of the main program.

  27. The return value, an integer between 0~256. The return value is saved to the variable $?.

  28. If no return value is specified, the returned function value is the exit state value of the last command executed in the function.

  29. Load function

  30. The definition of a function can be placed in a ~/.bash_profile file, either directly on the command line or in a script file.

  31. If you need to use a function that is saved in another script file, you can load them into memory by using the source command or the. command for use by the current script.

  32. Local temp defines a native variable

  33. As shown below, it is divided into two files a.sh and a1.sh. In a1.sh, use source a.sh to invoke a.sh.

  34.   1 #! /bin/bash  2  #filename:a.sh  3   4  function square  5 {  6   local temp  7    let temp=$1*$1  8   echo  "$1 square :  $temp"   9 } 10  11 function cube 12 { 13    local temp 14   let temp=$1*$1*$1 15   echo  "$1  cube :  $temp " 16 }  1 #! /bin/bash  2  #filename: a1.sh   3 source a.sh  4 echo  "Please enter an integer:"   5 read N  6 i=1  7 while [  $i  -le  $N  ]  8 do  9   square  $i  10   i=$ (($i + 1))  11 done 12 echo  "------------------"  13 i=1 14 while [  $i  -le  $N  ] 15  do 16   cube  $i  17   i=$ (($i + 1))  18 done#  bash a1.shplease enter an integer:31 square : 12 square : 43  square : 9------------------1 cube : 12 cube : 83 cube  : 27#
  35. Delete a function

  36. unset-f command to remove functions from shell Memory: unset-f function name

  37. DECLARE-F display of defined functions in memory



function scope

Scope, which is the range of variables. Scope that defines the visibility and life cycle of the variable.

    1. Global scope

      (1) The function runs in the current environment, sharing variables with the script that invokes it, i.e. variables that are not specifically declared with the keyword local, both inside and outside the function, have global scope .

      (2) Global variables can be accessed from anywhere in the program.


    2. Local scope

      (1) A variable declared in a function using the local keyword is called a local variable of the function.

      (2) The scope of the local variable is limited to the function.

      (3) When a local variable is output outside the local variable, the output is empty.

    3. When the global scope overlaps with the local scope, it is handled according to the nearest principle.

    4. Shell supports nesting of functions, that is, defining and invoking other functions in a function


Recursion of functions

    1. Recursion means to use your own definition to describe yourself.

    2. Recursive functions, especially for browsing dynamic data structures, such as lists and trees.

    3. To avoid a dead loop, when designing a recursive function, there must be a definite and necessarily occurring condition to terminate the recursive invocation of the function.

    4. In many cases, recursion can be replaced by loops, because both do something repetitive.

    5. Recursive calls are slower than loops and consume more memory, so use loops as much as possible.

    6. Example

    7. 1 #! /bin/bash 2 #filename: a.sh 3 Function Reverse 4 {5 Local T 6 echo $7 If [$1-GT 0]; then 8 t=$ (($1-1 )) 9 Reverse $t ten fi reverse 10


Shell Note 8

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.