• shell_74.Linux创建使用函数


    创建函数
    在 bash shell 脚本中创建函数的语法有两种
    第一种使用关键字 function,随后跟上分配给该代码块的函数名:

    1. function name
    2.  commands
    3. }

    name 定义了该函数的唯一名称。脚本中的函数名不能重复。
    commands 是组成函数的一个或多个 bash shell 命令。调用该函数时,bash shell 会依次执行函数内的命令,就像在普通脚本中一样。

    第二种在 bash shell 脚本中创建函数的语法更接近其他编程语言中定义函数的方式:

    1. name() { 
    2. commands


    函数名后的空括号表明正在定义的是一个函数。这种语法的命名规则和第一种语法一样。


     使用函数
    要在脚本中使用函数,只需像其他 shell 命令一样写出函数名即可:

    1. $ cat test1 
    2. #!/bin/bash 
    3. # using a function in a script 
    4. function func1
    5.      echo "This is an example of a function" 
    6. count=1 
    7. while [ $count -le 5
    8. do 
    9.     func1 
    10.     count=$[ $count + 1
    11. done 
    12. echo "This is the end of the loop" 
    13. func1 
    14. echo "Now this is the end of the script" 
    15. $ ./test1 
    16. This is an example of a function 
    17. This is an example of a function 
    18. This is an example of a function 
    19. This is an example of a function 
    20. This is an example of a function 
    21. This is the end of the loop 
    22. This is an example of a function 
    23. Now this is the end of the script 
    24. $

    函数定义不一定非要放在 shell 脚本的最开始部分,但是要注意这种情况。如果试图在函数被定义之前调用它,则会收到一条错误消息:

    1. $ cat test2 
    2. #!/bin/bash 
    3. # using a function located in the middle of a script 
    4. count=1 
    5. echo "This line comes before the function definition" 
    6. function func1
    7.     echo "This is an example of a function" 
    8. while [ $count -le 5
    9. do 
    10.     func1 
    11.      count=$[ $count + 1
    12. done 
    13. echo "This is the end of the loop" 
    14. func2 
    15. echo "Now this is the end of the script" 
    16. function func2
    17.     echo "This is an example of a function" 
    18. $ ./test2 
    19. This line comes before the function definition 
    20. This is an example of a function 
    21. This is an example of a function 
    22. This is an example of a function 
    23. This is an example of a function 
    24. This is an example of a function 
    25. This is the end of the loop 
    26. ./test2: func2: command not found 
    27. Now this is the end of the script 


    函数名必须是唯一的,否则就会出问题。如果定义了同名函数,
    那么新定义就会覆盖函数原先的定义,而这一切不会有任何错误消息:

    1. $ cat test3 
    2. #!/bin/bash 
    3. # testing using a duplicate function name
    4. function func1
    5.     echo "This is the first definition of the function name" 
    6. func1 
    7. function func1
    8.  echo "This is a repeat of the same function name" 
    9. func1 
    10. echo "This is the end of the script" 
    11. $ ./test3 
    12. This is the first definition of the function name 
    13. This is a repeat of the same function name 
    14. This is the end of the script 


     

  • 相关阅读:
    flinkdashboard未授权
    【JavaEE】Cookie和Session详解
    A Survey on Fairness in Large Language Models
    图文总结:正向代理与反向代理
    1.3-17:计算三角形面积
    【数据库原理及应用】——关系数据库的规范化理论(学习笔记)
    计算机毕业设计(附源码)python中小型企业工作日志管理系统APP
    在virtualbox上搭建kubenetes集群入门
    2、TCP协议基础
    luajit开发文档wiki中文版(二) LuaJIT 扩展
  • 原文地址:https://blog.csdn.net/mmmmm168m/article/details/134258550