1. 函数创建与使用

创建格式:

function name {                                     name() {

commands                或                         commands

}                                                             }

调用:  直接写函数名就可以调用

 1 [Hermioner@localhost Documents]$ cat test2.sh
 2 #!/bin/bash
 3 func1() {
 4     lss
 5     echo "hello"
 6 }
 7 func1
 8 echo $?
 9 [Hermioner@localhost Documents]$ bash test2.sh
10 test2.sh: line 3: lss: command not found
11 hello
12 0
13 [Hermioner@localhost Documents]$

View Code

note:用标准变量$?来确定函数的退出状态码,它默认是返回的是函数中的最后一条命令的退出状态码,即使函数中间有错误。----------很危险

可以有两种办法来改进:return 和使用函数输出

(1)使用return命令

函数调用和状态码输出语句中什么别的语句都不添加的话,函数调用的返回结果将代替状态码中的值,否则,就会返回最后一条语句的状态码。

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4     read -p "Please enter a value:" value
 5     echo "double the value"
 6     return $[ $value*2 ]
 7 }
 8
 9 f1
10
11 echo "The new value is $?"
12 [Hermioner@localhost Documents]$ bash test1.sh
13 Please enter a value:3
14 double the value
15 The new value is 6
16 [Hermioner@localhost Documents]$ bash test1.sh
17 Please enter a value:200
18 double the value
19 The new value is 144
20 [Hermioner@localhost Documents]$
21 #这种情况函数的返回值作为$?的值,但是任何大于256的值都会产生一个错误的值,函数的返回值不可以大于256.因为退出状态码的值必须是0-255.

View Code

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4     read -p "Please enter a value:" value
 5     echo "double the value"
 6     return $[ $value*2 ]
 7 }
 8
 9 f1
10 echo "hello"
11 echo "The new value is $?"
12 [Hermioner@localhost Documents]$ bash test1.sh
13 Please enter a value:3
14 double the value
15 hello
16 The new value is 0
17 #这种情况是函数的返回值并没有在$?显示出来

View Code

(2)使用函数输出----整数,浮点,字符串都适用

可以将函数的输出保存在变量中。

 1 [Hermioner@localhost Documents]$ cat test2.sh
 2 #!/bin/bash
 3 function f2 {
 4      read -p "Pleas enter a value: "  value
 5      echo $[ $value*2 ]
 6 }
 7 result=$(f2)
 8 echo "The reuslt is $result"
 9 [Hermioner@localhost Documents]$ bash test2.sh
10 Pleas enter a value: 3
11 The reuslt is 6
12 [Hermioner@localhost Documents]$

View Code

2.  在函数中使用变量

(1)向函数传递参数

在脚本中指定函数时,必须将参数和函数放在同一行。

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4     echo $1
 5 }
 6 #value=$(f1 $1)
 7 value=$(f1)
 8 echo "the new value is $value"
 9
10
11 [Hermioner@localhost Documents]$ bash test1.sh
12 the new value is
13 [Hermioner@localhost Documents]$ bash test1.sh 3
14 the new value is
15 #脚本中在调用函数时没有使用$1

View Code

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4     echo $1
 5 }
 6 value=$(f1 $1)
 7 #value=$(f1)
 8 echo "the new value is $value"
 9
10
11 [Hermioner@localhost Documents]$ bash test1.sh
12 the new value is
13 [Hermioner@localhost Documents]$ bash test1.sh 3
14 the new value is 3
15 #在函数调用时采用了$1传入参数

View Code

note:必须在脚本中也手动传参

(2)在函数中处理变量

全局变量和局部变量的处理。在函数内部变量或者变量赋值前面写上local,就可以当作局部变量处理,否则,默认当成全局变量处理。

全局变量在shell脚本中任何地方都是有效的变量。如果你在脚本的主体部分定义了一个全局变量,那么可以在函数内读取它的值。类似地,如果你在函数内定义了一个全局变量,可以在脚本的主体部分读取它的值。

默认情况下,你在脚本中定义的任何变量都是全局变量。在函数外定义的变量可以在函数内正常访问。

 1 [Hermioner@localhost Documents]$ cat htest.sh
 2 #!/bin/bash
 3 function f1{
 4 temp=$[ $value + 5 ]
 5 result=$[ $temp * 2 ]
 6 }
 7 temp=4
 8 value=6
 9 f1
10 echo "The result is $result"
11 if [ $temp -gt $value ]
12 then
13     echo "temp is larger"
14 else
15     echo "temp is smaller"
16 fi
17
18
19 ./htest.sh
20 The result is 22
21 temp is larger
22 如果在temp前面加了local,输出为:
23 The result is 22
24 temp is smaller

View Code

3. 数组变量和函数

(1)向函数传数组参数

类似变量传入

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4    local newarry
 5    newarry=(echo $@)
 6    echo "The new arry value is:  ${newarry[*]}"
 7 }
 8 myarry=(1 2 3 4 5)
 9 echo "The original array is ${myarry[*]}"
10 #f1 ${myarry[*]}
11 f1 $myarry
12 [Hermioner@localhost Documents]$ bash test1.sh
13 The original array is 1 2 3 4 5
14 The new arry value is:  echo 1
15 [Hermioner@localhost Documents]$ 

View Code

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f1 {
 4    local newarry
 5    newarry=(echo $@)
 6    echo "The new arry value is:  ${newarry[*]}"
 7 }
 8 myarry=(1 2 3 4 5)
 9 echo "The original array is ${myarry[*]}"
10 f1 ${myarry[*]}
11 #f1 $myarry
12 [Hermioner@localhost Documents]$ bash test1.sh
13 The original array is 1 2 3 4 5
14 The new arry value is:  echo 1 2 3 4 5
15 [Hermioner@localhost Documents]$ 

View Code

(2)从函数返回数组

参考博客: https://blog.csdn.net/guizaijianchic/article/details/78012179

 4. 函数递归

比如计算4的阶乘    x!=x*(x-1)!

 1 [Hermioner@localhost Documents]$ cat test1.sh
 2 #!/bin/bash
 3 function f {
 4      if [ $1 -eq 1 ]
 5      then
 6          echo 1
 7      else
 8          local temp=$[ $1-1 ]
 9          local result=$(f $temp)
10          echo $[ $result*$1 ]
11      fi
12 }
13 read -p "Please enter a value:  " value
14 result=$(f $value)
15 echo "The final result is: $result"
16 [Hermioner@localhost Documents]$ bash test1.sh
17 Please enter a value:  4
18 The final result is: 24
19 [Hermioner@localhost Documents]$ 

View Code

5. 创建库

将常用的函数定义成库函数,这样在不同的目录下可以调用这个函数。

1 [Hermioner@localhost ~]$ bash testfile
2 the result is 25
3 [Hermioner@localhost ~]$ cat testfile
4 #!/bin/bash
5 source /home/Hermioner/Documents/myfuncs
6 result=$(addem 10 15)
7 echo "the result is $result"

View Code

其中的source可以用点号代替。注意路径一定要对。

note:也可以在命令行上创建函数,但是一旦退出shell,这个函数就消失了。为了避免这个缺点,可以在.bashrc文件中来自定义库函数。但是这个文件中已经又发行版本定义好的库函数,小心操作。

参考文献

Linux命令行与shell脚本编程大全(第3版)[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译

转载于:https://www.cnblogs.com/Hermioner/p/9390276.html

shell基础07 函数相关推荐

  1. Python基础07 函数

    函数最重要的目的是方便我们重复使用相同的一段程序. 将一些操作隶属于一个函数,以后你想实现相同的操作的时候,只用调用函数名就可以,而不需要重复敲所有的语句. 函数的定义 首先,我们要定义一个函数, 以 ...

  2. 自定义函数_python3基础07函数(自定义)

    "pythonic生物人"的第43篇分享. 详细介绍python中:自定义函数的构建:参数传递:模块中调用函数. 目录 0.楔子 1.自定义函数格式 2.编写函数说明文档 3.函数 ...

  3. linux shell 基础 使用日志与心得

    linux shell 基础 使用日志与心得 1.#!/bin/bash 第一行就出现 #!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释 ...

  4. centos shell基础 alias 变量单引号 双引号 history 错误重定向 21 jobs 环境变量 .bash_history source配置文件 nohup ...

    centos shell基础知识 alias  变量单引号 双引号   history 错误重定向 2>&1  jobs  环境变量 .bash_history  source配置文件 ...

  5. 一、Linux Shell基础

    1.1.shell基础 Bash 是一个与Bourne Shell兼容的.执行从标准输入设备文件读取的命令的语言解释器.Bash是Bournae-Again Shell的缩写.Bash与原来的Unix ...

  6. shell日期处理函数

    ###################################### #SHELL日期计算函数 # #1:判断是否闰年check_leap() # #2:获取月份最大日期get_mon_day ...

  7. 初学者也能看懂的 Vue2 源码中那些实用的基础工具函数

    1. 前言 大家好,我是若川.最近组织了源码共读活动,感兴趣的可以加我微信 ruochuan12 想学源码,极力推荐之前我写的<学习源码整体架构系列>jQuery.underscore.l ...

  8. Linux shell篇---之一--shell基础

    一.shell基础 1.shell的基本概念 shell就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具. 用户界面shell(还有其他用户界面如kde等图形界面)-->内核--& ...

  9. Shell 基础知识--细说linux配套视频

    Shell 基础概括 Shell是什么? shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至是编写一些程序 ...

最新文章

  1. 2014 百度之星题解 1002 - Disk Schedule
  2. Chkconfig命令
  3. 数据库防护技术对比分析
  4. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
  5. 由mysql分区想到的分表分库的方案
  6. 产品经理版知乎竟成招聘利器,某公司当天收到200+简历
  7. Java中重载和复写的区别
  8. html中的空格表示
  9. SQL Server “复制”表结构,创建_Log表及触发器
  10. HDU4405(期望DP)
  11. pytorch 动态图机制
  12. java 命令行 库_java以太坊库web3j文档
  13. fgo最新服务器,FGO服务器故障追加说明 凌晨3点已开服
  14. 人工智能权威网站推荐
  15. 创建hive的AES加密解密函数
  16. matlab判断系统稳定性 -Nyquist图(极坐标图)判据(还没有搞完。。。。。。。)
  17. C#中判断空字符串的3种方法性能分析 1
  18. 2020,那些惊艳我的产品迭代
  19. Bloxorz I POJ - 3322 bfs
  20. 5款超级好用的开发效率工具,建议收藏!

热门文章

  1. in band out of band
  2. DTMF采用RFC2833进行带内传输的实现[ZT]
  3. oSIP开发者手册 (三)
  4. scrapy爬虫架构介绍和初试
  5. OpenGL(三)——OpenGL着色器基础
  6. oracle 临时表空间满了_精心总结--Oracle查询表空间的每日增长量和历史情况统计脚本...
  7. go调用ffmpeg
  8. mysql foreign key_MYSQL外键(Foreign Key)的使用
  9. python定义变量_Python基础 变量的基本使用
  10. 【clickhouse】flink jdbc 方式写入 clickhouse 报错 request to {}->http://xxx:8123: Broken pipe