函数是被赋予名称的脚本代码块,可以在代码的任意位置重用。每当需要在脚本中使用这样的代码块时,只需引用该代码块被赋予的函数名称。

创建函数

格式

function name {

  commands

}

name 属性定义了该函数的唯一名称。name 后面要有空格。

commands 是组成函数的一条或多条 bash shell 命令。

另一种格式

name() {

  commands

}

示例

#!/bin/bash
# using a function in a script

function func1 {
  echo "This is an example of a function"
}

count=1
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done

echo "This is the end of the loop"
func1
echo "Now this is the end of the script"

[root@tang sh14]# ./test1
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
Now this is the end of the script

注意:如果在函数定义之前使用函数,会得到错误消息。

如果重新定义函数,那么新定义将取代函数原先的定义。

函数返回值

默认退出状态

默认情况下,函数的退出状态是函数的最后一条命令返回的退出状态。

示例

#!/bin/bash
# testing the exit status of a function

func1() {
  echo "trying to display a non-existent file"
  ls -l badfile
}

echo "testing the function:"
func1
echo "The exit status is: $?"

[root@tang sh14]# ./test4
testing the function:
trying to display a non-existent file
ls: badfile: No such file or directory
The exit status is: 2

使用 return 命令

return  命令可以使用单个整数值来定义函数退出状态,提供了一种通过编程设置函数退出状态的简单方法。

示例

#!/bin/bash
# using the return command in a function

function db1 {
  read -p "Enter a value: " value
  echo "doubling the value"
  return $[ $value * 2]
}

db1
echo "The new value is $?"

[root@tang sh14]# ./test5
Enter a value: 10
doubling the value
The new value is 20

注意:请记住在函数完成后尽快提取返回值

请记住退出状态的取值范围是0~255

使用函数输出

正如命令输出可以捕获并存放到 shell 变量中一样,函数的输出也可以捕获并存放到 shell 变量中。

示例

#!/bin/bash
# using the echo to return a value

function db1 {
  read -p "Enter a value: " value
  echo $[ ($value) * 2 ]
}

result=`db1`
echo "The new value is $result"

[root@tang sh14]# ./test5b
Enter a value: 1
The new value is 2

向函数传递参数

函数可以使用标准参数环境变量来表示命令行传递给函数的参数。

函数名在变量 $0 中定义, 函数命令行的其他参数使用变量 $1、$2...,专有变量$#可以用来确定传递给函数的参数数目。

示例

#!/bin/bash
# passing parameters to a function

function addem {
  if [ $# -eq 0 ] || [ $# -gt 2 ]
  then
    echo -1
  elif [ $# -eq 1 ]
  then
    echo $[ $1 + $1 ]
  else
    echo $[ $1 + $2 ]
  fi
}

echo -n "Adding 10 and 15: "
value=`addem 10 15`
echo $value
echo -n "Let's try adding just one number: "
value=`addem 10`
echo $value
echo -n "Now trying adding no numbers: "
value=`addem`
echo $value
echo -n "Finally, try adding three numbers: "
value=`addem 10 15 20`
echo $value

[root@tang sh14]# ./test6
Adding 10 and 15: 25
Let's try adding just one number: 20
Now trying adding no numbers: -1
Finally, try adding three numbers: -1

由于函数为自己的参数值使用专用的参数环境变量,所以函数无法从脚本命令行直接访问脚本参数值。如果想在函数中使用这些值,那么必须在调用该函数时手动传递这些数据。

示例

#!/bin/bash
# trying to access script parameters inside a function

function func7 {
  echo $[ $1 * $2 ]
}

if [ $# -eq 2 ]
then
  value=`func7 $1 $2`
  echo "The result is $value"
else
  echo "Usage: badtest1 a b"
fi

[root@tang sh14]# ./test7
Usage: badtest1 a b

[root@tang sh14]# ./test7 2 6
The result is 12

在函数中使用变量

全局变量是在 shell 脚本内处处有效的变量。

示例

#!/bin/bash
# using a global variable to pass a value

function db1 {
  value=$[ $value * 2 ]
}

read -p "Enter a value: " value
db1
echo "The new value is: $value"

[root@tang sh14]# ./test8
Enter a value: 10
The new value is: 20

局部变量是在函数内部使用的变量,在变量声明前面冠以 local 关键字。

如果脚本在函数外部有同名变量,那么 shell 将能区分开这两个变量。

示例

#!/bin/bash
# demonstrating the local keyword

function func1 {
  local temp=$[ $value + 5 ]
  result=$[ $temp * 2 ]
}

temp=4
value=6

func1
echo "The result is $result"

if [ $temp -gt $value ]
then
  echo "temp is larger"
else
  echo "temp is smaller"
fi

[root@tang sh14]# ./test9
The result is 22
temp is smaller

向函数传递数组

如果试图将数组变量作为函数参数使用,那么函数只提取数组变量的第一个取值。

示例

#!/bin/sh
# array variable to function test

function testit {
  local newarray
  newarray=("$@")
  echo "The new array value is: ${newarray[*]}"
}

myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
testit ${myarray[*]}

[root@tang sh14]# ./test10
The original array is 1 2 3 4 5
The new array value is: 1 2 3 4 5

必须将数组变量拆分为单个元素,然后使用这些元素的值作为函数参数。

函数内部使用数组

示例

#!/bin/bash
# adding values in an array

function addarray {
  local sum=0
  local newarray
  newarray=(`echo "$@"`)
  for value in ${newarray[*]}
  do
    sum=$[ $sum + $value ]
  done
  echo $sum
}

myarray=(1 2 3 4 5)
echo "The original array is: ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=`addarray $arg1`
echo "The result is $result"

[root@tang sh14]# ./test11
The original array is: 1 2 3 4 5
The result is 15

从函数返回数组

示例

#!/bin/sh
# returning an array value

function arraydblr {
  local origarray
  local newarray
  local elements
  local i
  origarray=(`echo "$@"`)
  newarray=(`echo "$@"`)
  elements=$[ $# - 1 ]
  for(( i=0; i<=elements; i++ ))
  {
    newarray[$i]=$[ ${origarray[$i]} * 2 ]
  }
  echo ${newarray[*]}
}

myarray=(1 2 3 4 5)
echo "The original array is: ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=(`arraydblr $arg1`)
echo "The new array is: ${result[*]}"

[root@tang sh14]# ./test12
The original array is: 1 2 3 4 5
The new array is: 2 4 6 8 10

函数递归

示例

#!/bin/sh
# using recursion

function factorial {
  if [ $1 -eq 1 ]
  then
    echo 1
  else
    local temp=$[ $1 - 1 ]
    local result=`factorial $temp`
    echo $[ $1 * $result ]  
  fi
}

read -p "Enter value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"

[root@tang sh14]# ./test13
Enter value: 5
The factorial of 5 is: 120

创建库

创建函数的库文件,可以在不同脚本中引用该库文件。

其难点在于 shell 函数的作用域。与环境变量一样,shell 函数仅在其定义所处的 shell 会话中有效。如果从 shell 命令行界面运行库文件,那么 shell 将打开一个新的 shell ,并在新 shell 中运行此脚本,但是当试图运行调用这些库函数的另一个脚本时,库函数并不能使用。

使用函数库的关键是 source 命令。source 命令在当前 shell 环境中执行命令,而非创建新 shell 来执行命令。使用 source 命令在 shell 脚本内部运行库文件脚本。

source 有一个短小的别名,称为点操作符(.)

示例

#!/bin/bash
# using functions defiend in a library file

. ./myfuncs

value1=10
value2=5
result1=`addem $value1 $value2`
result2=`multem $value1 $value2`
result3=`divem $value1 $value2`

echo "The result of adding them is: $result1"
echo "The result of multiplying them is: $result2"
echo "The result of dividing them is: $result3"

[root@tang sh14]# ./test14
The result of adding them is: 15
The result of multiplying them is: 50
The result of dividing them is: 2

在命令行中使用参数

$ function divem { echo $[ $1 + $2 ]; }

$ divem 100 5

在.bashrc 文件中定义函数

将函数定义放在 shell 每次启动都能重新载入的地方,.bashrc。

可以直接定义函数,或者提供函数文件。

转载于:https://www.cnblogs.com/ivantang/p/4021427.html

Shell 语法之函数相关推荐

  1. shell语法 函数

    函数介绍 函数就是用来盛放一组代码的容器,函数内的一组代码完成一个特定的功能,称之为一组代码块,调用函数便可触发函数内代码块的运行,这可以实现代码的复用,所以函数又可以称之为一个工具. 为何要用函数 ...

  2. shell 语法简介(转载)

    一.基本语法 1.1.shell文件开头 shell文件必须以下面的行开始(必须方在文件的第一行):  #!/bin/sh  符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使 ...

  3. shell中的函数及脚本调试方法

    1.函数格式 目的:将一些相对对立的代码变成函数,提供可读性和重用性,避免重复编写相同代码. 函数格式: 函数关键字:function可以省略,可不带任何参数; 符号{表示函数体的开始(可在函数名后单 ...

  4. editplus配置 linux shell 语法高亮 自动补全

    1.文件下载 我们可以从EditPlus官网的User Files获得不同语言的语法高亮和自动补全文件. Bash版本下载地址为:http://www.editplus.com/dn.php?n=ba ...

  5. shell语法简单介绍

    一.基本的语法 1.1.shell文件开头 shell文件必须以以下的行開始(必须方在文件的第一行):  #!/bin/sh  符号#!用来告诉系统它后面的參数是用来运行该文件的程序.在这个样例中我们 ...

  6. linux的基础知识——shell语法

    文章目录 1.shell语法--条件测试 1.1 条件测试 1.2 条件测试 2.shell语法--分支语句 3.shell语法--case分支语句 4.shell语法--foreach循环 5.sh ...

  7. 【Note4】shell语法,ssh/build/scp/upgrade,环境变量,自动升级bmc,bmc_wtd,peci,软连接

    文章目录 1.shell语法:Shell是用C语言编写的程序,它是用户使用Linux的桥梁,硬件>内核(os)>shell>文件系统 1.1 变量:readonly定义只读变量,un ...

  8. python实用的语法和函数

    本文介绍了python中实用的一些语法和函数 持续更新 代码较长 1215行 建议保存源码 遇到有困惑的时候 在代码中Ctrl+F寻找答案 Gitee源码 Github源码 目录: 版本查看 pip ...

  9. Shell语法详解专栏目录

    Shell语法详解专栏 1. [Shell详解-1]:概论.注释 2. [Shell详解-2]:变量.默认变量.数组 3. [Shell详解-3]:expr命令.read命令 4. [Shell详解- ...

最新文章

  1. AI中pass架构设计优化
  2. centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网...
  3. 已知bug列表——Solidity中文文档(12)
  4. 7.26T1四分图匹配
  5. 行家来信 | 功能安全会成为自动驾驶的紧箍咒吗?
  6. linux-windows主动推送文件同步目录数据 linux-windows数据目录同步
  7. 管你MySQL还是Oracle,数据库管理就完事了
  8. modelsim的库仿真流程--1
  9. 关于函数,对象以及闭包的一些理解
  10. python编程规则_python编程规则
  11. 业务用例模型涉及的主要概念
  12. Linux LVM的PV操作
  13. iOS 打开扬声器以及插入耳机的操作
  14. 赴日本常见问题QA (2转 不明原处)
  15. win10找不到便签(便利贴)怎么办,Win10找回便签功能的方法
  16. 《大明王朝》阴谋诡计,下三路招呼
  17. 辅助分类器遇上Domain Adaptation:连续性与不确定性
  18. 进击的 Vulkan 移动开发(一)之今生前世
  19. 用计算机弹我的一个道故朋友,我的一个道姑朋友
  20. 文件is not found in the curren directory or on the Matlab path

热门文章

  1. netty9---使用编码解码器
  2. kotlin教程(1)
  3. Html.RenderPartial使用三个参数
  4. Hibernate 多表关联
  5. DataTable筛选某列最大值
  6. leetcode - First Missing Positive
  7. Python爬虫_Requests
  8. JQuery源码笔记jQuery.access研究学习(13)
  9. css3实现的the Sexy Buttons
  10. jQuery实现 div里面的文字如何自动缩小,避免文字溢出