使用函数

#!/bin/bash
# testing the script function myfun {
echo "This is an example of a function"
} count=1
while [ $count -le 5 ]
do
myfun
count=$[ $count +1 ]
done
echo "This is the end of the loop"
myfun
echo "Now this is the end of the script"

(记得空格,函数一定要在使用之前定义,函数名必须唯一)

返回值
可以通过$?来确定函数的退出状态

使用return命令

#!/bin/bash
# testing the script
function myfun {
read -p "Enter a value:" value
echo "double the value"
return $[ $value * 2 ]
} myfun echo "The new vlue is $?"

结果:
Enter a value:23
double the value
The new vlue is 46
(退出状态的取值范围是0到255,$?是最近已执行命令的退出状态)

使用函数输出,这种方法最靠谱了
看例子

#!/bin/bash
# testing the script
function myfun {
read -p "Enter a value:" value
echo $[ $value * 2 ]
} result=`myfun` echo "The new vlue is $result"

(这样就能很好的利用输出结果了)

在函数中使用变量
向函数传递参数

#!/bin/bash
# testing the script
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 "Just one number 10:"
value=`addem 10`
echo $value
echo -n "No numbers:"
value=`addem`
echo $value
echo -n "More than two numbers:"
vaule=`addem 10 15 20`
echo $value

结果:
Adding 10 and 15:25
Just one number 10:20
No numbers:-1
(由上述可知,#可以得到参数的个数,#可以得到参数的个数,1表示第一个参数,$2表示第二个参数)

#!/bin/bash
# testing the script
function addem {
if [ $# -eq 0 ]||[ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
} if [ $# -eq 2 ]
then
value=`addem $1 $2`
echo "The value is $value"
else
echo "Usage:test1 a b"
fi

(函数外面的#表示调用脚本使用的参数数,函数里的#表示调用脚本使用的参数数,函数里的#表示调用函数使用的参数数,注意这点区别)

作用域是变量的可见区域。
全局变量,在shell脚本内处处有效的变量。函数中定义全局变量,那么主代码中有效。主代码中定义全局,函数中也有效。
默认情况下,脚本中定义的变量都是全局变量。
看例子:

#!/bin/bash
# testing the script
function myfun {
value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"

(输入45,得到90,这里的value在函数中发生变化了,到脚本中一样可以使用,而且已经变化了)

局部变量
作用范围只在函数当中
关键字local

#!/bin/bash
# testing the script
function myfun {
local value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"

(输入45,输出45。因为加上local关键字之后,函数中的value是局部变量,与外界无关)

#!/bin/bash
# testing the script
function myfun {
local value=$[ $value * 2 ]
echo $value
}
read -p "Enter a value:" value
result=`myfun`
echo "The new value is:$value"
echo "The result of the fun is $result"

(不过可以通过输出来获取函数处理的结果,这里的result还是处理后的结果,比如输入45,处理后是90)

数组变量与函数

#!/bin/bash
# testing the script
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[*]}"
arg=`echo ${myarray[*]}`
result=`addarray $arg`
echo "The result is $result"

结果:
The original array is :1 2 3 4 5
The result is 15
(数组参数传入函数,函数获取后,进行处理输出。脚本将输出结果打印)

#!/bin/bash
# testing the script
function addarray {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo ${newarray[*]}
}
myarray=(1 2 3 4 5)
echo "The original array is :${myarray[*]}"
arg=`echo ${myarray[*]}`
result=`addarray $arg`
echo "The result is $result"

(输出数组)

函数递归

#!/bin/bash
# testing the script
function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 -1 ]
local result=`factorial $temp`
echo $[ $result * $1 ]
fi
} read -p "Enter value:" value
result=`factorial $value`
echo "The factorial of $value is:$result"

(输入5,输出120。大环套小环,小环套更小,逐步执行。
我们一步一步剖析
5输入得到5*4!
4又得到4*3!
3又得到3*2!
2又得到2*1
由此得到5*4*3*2*1也就是120

显然单个脚本有助于减少输入量,但是如果多个脚本碰巧使用同样的函数又该怎样?
创建库

#my script functions
function addem {
echo $[ $1 + $2 ]
} function multem {
echo $[ $1 * $2 ]
} function divem {
if [ $2 -ne 0 ]
then
echo $[ $1 / $2 ]
else
echo -1
fi
}

使用函数库的关键词是source

#!/bin/bash
# testing the script
source ./myfuncs result=`addem 10 15`
echo "The result is $result"

(通过source就能使用库函数了)

#!/bin/bash
# testing the script
. ./myfuncs

result=`addem 10 15`
echo "The result is $result"
(或者点操作符,效果是一样的)

在命令行中使用函数
[root@localhost shellscript]# function multem {

> echo [1*$2]

> }

[root@localhost shellscript]# multem 2 5

10
(多行命令)

在.bashrc文件中定义函数
直接在命令行定义shell函数的缺点是一旦退出shell,函数定义将失效。
比较省事的方法是将函数定义在shell每次启动都能重新载入的地方。.bashrc文件就是这样的地方。
可以用source(点操作符)将现有库文件的函数包含进.bashrc脚本。
重新启动命令(source .bashrc)
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

function addem {
  echo [1 + $2 ]
}
(重启之后,新加入的函数就有效了)

脚本中也可以使用了

~~~~~~~~~~~~

文章出处:http://www.cnblogs.com/jiqing9006/p/3253232.html

~~~~~~~~~~~~~

感谢作者的总结

linux shell 进阶篇、shell脚本编程-创建函数的更多相关文章

  1. Linux&shell之高级Shell脚本编程-创建函数

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用函数 #!/bin/bash # testing the script function myfun { echo "This i ...

  2. Shell编程—创建函数

    1基本的脚本函数 函数是一个脚本代码块,你可以为其命名并在代码中任何位置重用.要在脚本中使用该代码块时,只要使用所起的函数名就行了. 1.1创建函数 有两种格式可以用来在bash shell脚本中创建 ...

  3. linux awk进阶篇

    上一篇主要是awk的进本应用.本节是awk的进阶篇 ACTION:除去常用的print和printf还有以下几个 expression:表达式 如$1>3 control statements: ...

  4. javascript进阶——分离式DOM脚本编程

    编写分离式(unobstrusive)代码意味着对HTML内容的完全分离:数据来自服务器端,javascript代码用来动态化和交互.这种分离的好处是在不同浏览器之间使用是可以完全降级或升级运行,对于 ...

  5. labview从入门到出家5(进阶篇)--程序调试以及labview函数库的运用

    跟了前面几章的操作流程,相信大家对labview有了一定的认识.其实只要了解了labview的编程思路,再熟悉地运用各个变量,函数以及属性,那么我们就可以打开labview的大门了.跟其他编程语言一样 ...

  6. mysql编程--创建函数出错的解决方案

    本文章转载自:http://www.jb51.net/article/71100.htm 在使用MySQL数据库时,有时会遇到MySQL函数不能创建的情况.下面就教您一个解决MySQL函数不能创建问题 ...

  7. Linux&shell之高级Shell脚本编程-创建菜单

    写在前面:案例.常用.归类.解释说明.(By Jim) 创建菜单#!/bin/bash# testing the scriptclearechoecho -e "\t\t\tSys Admi ...

  8. shell进阶篇之字典和数组结合应用案例

    # 现在我们用字典结合数组来实现一个简单的远程管理机 远程管理机的需求:现在需要在一个管理机上实现下列两点内容: 1.需要可以实时查看现有项目运行状态 2.远程登陆任意一台机器 备注:现有的机器如下 ...

  9. shell进阶篇之数组应用案例

    数组中可以存放多个值. Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小. 与大部分编程语言类似,数组元素的下标由0开始. Shell 数组用括号来表示,元素用"空格 ...

随机推荐

  1. nGrinder TestRunner XFF / X-Forwarded-For

    s 我们在压测请求报文里面带了这个"x-forward-for":"10.24.51.132"这个字段,所以我们所有的压测请求穿透到应用系统的时候,应用系统上采 ...

  2. Spring项目集成ShiroFilter简单配置

    Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证.授权.加密.会话管理.与Web集成.缓存等功能.我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是 ...

  3. 15. 迭代器模式(Iterator Pattern)

    动机(Motivate):     在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历 ...

  4. python 深浅拷贝 for循环删除

    ###########################总结########################### 1. 基础数据类型补充 大多数的基本数据类型的知识.已经学完了 a='aaaa' ls ...

  5. Linux记录-sysctl.conf优化方案

    Sysctl是一个允许您改变正在运行中的Linux系统的接口.它包含一些 TCP/IP 堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能.用sysctl可以读取设置超过五百 ...

  6. Debugger for chrome

    Debugger In VScode Getting Started Install the extension Debugger for chrome Config the launch.json ...

  7. HDU 6432(不连续环排列 ~)

    题意是说在长度为 n 的环排列中,按照一定的方向(顺时针或逆时针),后一个数不能仅比前一个数大 1 , n 的下一个数不能是 1 ,问这种长度为 n 且本质不同(本质不同指环上数字的相对位置不同,如 ...

  8. poj2559/hdu1506 单调栈经典题

    我实在是太菜了啊啊啊啊啊 到现在连个单调栈都不会啊啊啊 写个经典题 #include<cstdio> #include<algorithm> #include<cstri ...

  9. EL 快速开始

    技术选型上,推荐使用EL表达式,少用不用taglib. 大趋势 前后端分离 mvc+mvvm ,使用[thymeleaf]和前端更好结合,也是springboot官方推荐的做法. [viewTicke ...

  10. window中安装mongodb

    转自:https://blog.csdn.net/heshushun/article/details/77776706 一.先登录Mongodb官网https://www.mongodb.com/do ...