Linux学习-高级shell脚本编程(一)函数的妙用
来源:互联网 发布:淘宝零信誉店铺出售 编辑:程序博客网 时间:2024/04/29 19:42
引文: 通常编写shell脚本时,你会发现很多地方都要用到相同的代码或者说是相同的功能。如果是一段小代码,那无所谓。可如果多次使用而且还是相同的代码,我想你也会感觉很烦的。为了能够让代码重用,这就使用到函数了。
温馨提示
变量赋值的格式为:
变量名=变量值
注意事项:
- 变量名前面不应加美元“$”符号。(和PHP不同)等号“=”
- 前后不可以有空格。和C语言不同,Shell中不需要显式的语法来声明变量。
- 变量名不可以直接和其他字符相连,如果想相连,必须用括号:echo “this is $(he)llo!”
函数定义格式
function name { commands}
或者是
name() {}
这个就和其他的语言有点类似了。
实例1.无参函数
一个简单的使用函数功能的shell脚本
test1.sh
#!/bin/bash# this is a test file for test functionfunction func1 { echo "this is an example of function!"}count=1echo "count's value is $count."while [ $count -le 5 ]do func1 count = $[ $count + 1 ]doneecho "end of while."
运行:
sh test1.sh
输出:
count’s value is 1.
this is an example of function!
this is an example of function!
this is an example of function!
this is an example of function!
this is an example of function!
end of while.
实例2.带参函数
test2.sh
#!/bin/bash# this is a test file for test functionfunction 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 5:"value=`addem 10 15`echo $valueecho -n "Let's try adding just one number:"value=`addem 10`echo $valueecho -n "Now trying adding no number:"value=`addem`echo $valueecho -n "Finally,we try adding three numbers:"value=`addem 10 15 20`echo $valueecho "end of file."
测试:
sh test2.sh
输出:
Adding 10 and 5:25
Let’s try adding just one number:20
Now trying adding no number:-1
Finally,we try adding three numbers:-1
end of file.
如果想在函数内部声明一个变量,可以使用local来定义,表示为局部变量。如local temp=1
。
实例3.数组参数
test3.sh
#!/bin/bash# trying to pass a array variablefunction testit { local newarray # use ‘local’ define a local variable newarray=(`echo "$@"`) echo "the newarray value is ${newarray[*]}"}myarray=(1,2,3,4,5)echo "the original array is:${myarray[*]}"testit $myarray
测试:
sh test3.sh
输出:
the original array is:1,2,3,4,5
the newarray value is 1,2,3,4,5
实例4.函数递归
定义阶乘函数x! = x * (x-1)
function factorial { if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1 - 1 ] local result=`factorial $temp` echo $[$result * $1] fi}
详细实例:test4.py
#!/bin/bash# using recursionfunction factorial { if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1 - 1 ] local result=`factorial $temp` echo $[ $result * $1 ] fi}
测试:
sh test4.py
输出:
Enter value:6
The factorial of 6 is: 720
实例5.命令行上使用函数命令
[root@master chapter16]# function multm {> echo $[ $1 * $2 ]> }[root@master chapter16]# multm 2 510
注意: 在命令行上定义函数时,如果你给函数起的名字跟内建命令or另一个命令相同名字,则函数将覆盖原来的命令。
如果想要开机生效,可以把函数卸载/etc/.bashrc文件中。
- Linux学习-高级shell脚本编程(一)函数的妙用
- 高级Bash脚本编程指南——一本深入学习shell脚本艺术的书籍
- 高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍
- 高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍
- Linux-Shell脚本编程-学习-8-函数
- linux shell高级脚本编程之函数篇
- linux shell脚本编程学习(一)基础
- 【Linux 系统编程】shell 脚本基础学习(一)
- Linux Shell编程四:高级shell脚本
- Linux下的shell脚本编程(一)
- shell 脚本编程学习(一) 基础
- Linux学习-高级shell脚本编程(二)初识sed和gawk
- shell 脚本编程学习(六) 函数
- 高级Shell 脚本编程学习笔记
- Linux学习(八):shell脚本编程
- linux shell脚本EOF妙用
- linux shell脚本EOF妙用
- 《Linux Shell脚本攻略》学习(一)
- 把php嵌入到c/c++里
- iOS-内存管理简单介绍
- 大话设计模式(二)代理模式
- IT面试网模拟Java面试过程分享第一篇
- 物料信息记录中几个常见的数据表
- Linux学习-高级shell脚本编程(一)函数的妙用
- ListView加载速度/性能优化方案分析
- 常用Oracle数据语句
- Facade in Java
- Spring 整合 Hessian
- SQL 拼音助记码
- flume log4j
- Ubuntu进入root权限命令——sudo和su命令详解
- 在iOS系统中使用OpenCV