linux shell 脚本,创建函数
来源:互联网 发布:知之深爱之切全文 编辑:程序博客网 时间:2024/06/07 12:00
使用函数
#!/bin/bash# testing the scriptfunction myfun { echo "This is an example of a function"}count=1while [ $count -le 5 ]do myfun count=$[ $count +1 ]doneecho "This is the end of the loop"myfunecho "Now this is the end of the script"
(记得空格,函数一定要在使用之前定义,函数名必须唯一)
返回值
可以通过$?来确定函数的退出状态
使用return命令
#!/bin/bash# testing the scriptfunction myfun { read -p "Enter a value:" value echo "double the value" return $[ $value * 2 ]}myfunecho "The new vlue is $?"
结果:
Enter a value:23
double the value
The new vlue is 46
(退出状态的取值范围是0到255,$?是最近已执行命令的退出状态)
使用函数输出,这种方法最靠谱了
看例子
#!/bin/bash# testing the scriptfunction myfun { read -p "Enter a value:" value echo $[ $value * 2 ]}result=`myfun`echo "The new vlue is $result"
(这样就能很好的利用输出结果了)
在函数中使用变量
向函数传递参数
#!/bin/bash# testing the scriptfunction 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 $valueecho -n "Just one number 10:"value=`addem 10`echo $valueecho -n "No numbers:"value=`addem`echo $valueecho -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 scriptfunction 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 scriptfunction myfun { value=$[ $value * 2 ]}read -p "Enter a value:" valuemyfunecho "The new value is:$value"
(输入45,得到90,这里的value在函数中发生变化了,到脚本中一样可以使用,而且已经变化了)
局部变量
作用范围只在函数当中
关键字local
#!/bin/bash# testing the scriptfunction myfun { local value=$[ $value * 2 ]}read -p "Enter a value:" valuemyfunecho "The new value is:$value"
(输入45,输出45。因为加上local关键字之后,函数中的value是局部变量,与外界无关)
#!/bin/bash# testing the scriptfunction myfun { local value=$[ $value * 2 ] echo $value}read -p "Enter a value:" valueresult=`myfun`echo "The new value is:$value"echo "The result of the fun is $result"
(不过可以通过输出来获取函数处理的结果,这里的result还是处理后的结果,比如输入45,处理后是90)
数组变量与函数
#!/bin/bash# testing the scriptfunction 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 scriptfunction 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 scriptfunction 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:" valueresult=`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 functionsfunction 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 scriptsource ./myfuncsresult=`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
> }
[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
}
(重启之后,新加入的函数就有效了)
脚本中也可以使用了
- linux shell 脚本,创建函数
- shell脚本--创建函数
- linux下创建shell脚本
- linux学习之shell脚本 ------- shell函数
- Linux命令与shell脚本编程学习笔记-16创建函数
- linux命令行与shell脚本大全_第十六章_创建函数
- Linux-Shell编程之创建shell脚本并执行
- shell脚本编程:Linux的自动化逻辑卷创建脚本
- Linux中shell脚本使用函数
- Linux命令行和shell脚本编程-函数
- Linux-Shell脚本编程-学习-8-函数
- linux shell脚本案例七:函数
- Linux shell脚本编程之函数
- shell脚本创建文件
- Linux shell脚本中,如何定义函数及调用函数
- 创建固定大小的文件 -- Linux shell 脚本编写实例
- linux系统下用shell脚本创建FTP帐号
- 创建固定大小的文件 -- Linux shell 脚本编写实例
- handler机制的原理
- Wireshark使用教程:第4章 实时捕捉数据包
- libsvm在python下的使用
- 读“一千万条以上记录分页数据库优化经验总结”想到的SqlServer smalldatetime与datetime
- (转)Windows下硬链接、软链接和快捷方式的区别
- linux shell 脚本,创建函数
- EXCEL使用技巧大全:输入的技巧
- Mybatis入门简单例子详解
- oracle中and的优先级要高于or的验证
- hadoop-hbase错误,搞了很久终于有结果了
- VC++中四种进程或线程同步互斥的控制方法
- Excel2007中数据分析功能详解
- 关于Oracle中的DRCP
- 常用的扒皮