【shell】shell编程(六)-shell函数的应用
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。
shell中函数的定义格式如下:
[ function ] funname [()]
{
action;
[return int;]
}
说明:
- 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
- 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)
例1:一个简单的例子:
#!/bin/bash
fun1(){
echo "this is my first function"
}
echo "function start"
fun1
echo "function end"
运行结果:
$ ./test.sh
function start
this is my first function
function end
例2:一个带有return语句的函数:
#!/bin/bash
funWithReturn(){
echo "这个函数会对输入的两个数字进行相加运算..."
echo "输入第一个数字: "
read aNum
echo "输入第二个数字: "
read anotherNum
echo "两个数字分别为 $aNum 和 $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"
输出类似下面:
这个函数会对输入的两个数字进行相加运算...
输入第一个数字: 输入第二个数字: 两个数字分别为 和 !
输入的两个数字之和为 !
函数返回值在调用该函数后通过 $? 来获得。
注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。
函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...
带参数的函数示例:
#!/bin/bash
funWithReturn(){
echo "this function have params"
echo "the first para is $1"
echo "the sec para is $2"
echo "the hir para is $3"
echo "the fourth para is $4"
} funWithReturn
结果:
$ ./test.sh
this function have params
the first para is
the sec para is
the hir para is
the fourth para is
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
| 参数处理 | 说明 |
|---|---|
| $# | 传递到脚本的参数个数 |
| $* | 以一个单字符串显示所有向脚本传递的参数 |
| $$ | 脚本运行的当前进程ID号 |
| $! | 后台运行的最后一个进程的ID号 |
| $@ | 与$*相同,但是使用时加引号,并在引号中返回每个参数。 |
| $- | 显示Shell使用的当前选项,与set命令功能相同。 |
| $? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
我们可以查看下面的例子:
#!/bin/bash
echo "\$# is $# "
echo "\$* is $* "
echo "\$$ is $$ "
echo "\$@ is $@ "
echo "\$? is $? "
echo "\$- is $- "
echo "\$3 is $3 "
echo "\$2 is ${2} "
echo "\$0 is $0 "
echo "\$0 is $(basename $0) "
结果:
$ ./test.sh
$# is
$* is
$$ is
$@ is
$? is
$- is hB
$ is
$ is
$ is ./test.sh
$ is test.sh
附一个启动webmin的脚本
#!/bin/bash
#start webmin service
start()
{
/etc/webmin/start >/dev/null > /dev/null
if [ $? = '' ]
then
echo "webmin is success start"
fi
} #stop webmin service
stop()
{
/etc/webmin/stop > /dev/null > /dev/null
if [ $? = '' ]
then
echo "webmin is success stop"
fi
} #status webmin
status()
{
/usr/bin/netstat -ano | /usr/bin/grep > /dev/null
if [ $? != '' ]
then
echo "webmin is not start"
else
echo "webmin is running"
fi
} ########read input##############
str=$
if [ ${str} = 'start' ]
then
start
elif [ ${str} = 'stop' ]
then
stop
elif [ ${str} = 'status' ]
then
status
else
echo "please use {start,stop,status}"
fi
附一个启动tomcat的脚本
#!/bin/bash
#chkconfig:
#description:tomcat
#input(start stop status) to operate tomcat service
#start funciton(start tomcat service use /opt/apache-tomcat/apache-tomcat-7.0./bin/start.sh)
export CATALINA_HOME=/opt/apache-tomcat/apache-tomcat-7.0.
start(){
/usr/bin/sh "${CATALINA_HOME}"/bin/startup.sh
if [ "$?" != "" ]
then
echo "service is not success start"
else
echo "service is success start" fi
exit
}
#stop function
stop(){
/usr/bin/sh "${CATALINA_HOME}"/bin/shutdown.sh
if [ "$?" != "" ]
then
echo "service is not success stop"
else
echo "service is success stop" fi
}
#status function
status(){
/usr/bin/ps -le | /usr/bin/grep java >/dev/null > /dev/null
if [ "$?" != "" ]
then
echo "service is not start"
else
echo "service is running" fi
}
#read input and dispose function
input=${}
case ${input} in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "please use {start to start tomcat,stop to stop tomcat,status to read tomcat status!}"
esac
【shell】shell编程(六)-shell函数的应用的更多相关文章
- shell的编程结构体(函数、条件结构、循环结构)
bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.1 shell函数 在shell中,函数可以被当作命令一样 ...
- linux中的shell脚本编程---初识shell
Shell是用户与Linux或Unix内核通信的工具,shell编程指的并不是编写这个工具,而是指利用现有的shell工具进行编程,写出来的程序是轻量级的脚本,我们叫做shell脚本. Shell的语 ...
- shell脚本编程基础之函数
函数 作用:代码重用 定义函数: 方法1: function FUNCTION_NAME { #函数名和定义变量名一样,只能包含数字字母下划线,并且不能以数字开头 command } 方法2: FUN ...
- C++面向对象高级编程(六)转换函数与non-explicit one argument ctor
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.conversion function 转换函数 //1.转换函数 //conversion function //只要你认为合理 你可以任 ...
- Shell脚本编程具体解释
第12章 Shell脚本编程 l Shell命令行的执行 l 编写.改动权限和运行Shell程序的步骤 l 在Shell程序中使用參数和变量 l 表达式比較.循环结构语句和条件结构语句 l ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记
第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...
- 《Linux命令行与shell脚本编程大全》第十六章 控制脚本
一些控制脚本的方式:向脚本发送信号.修改脚本优先级,在脚本运行时切换到运行模式 16.1 处理信号 linux利用信号与运行在系统中的进程进行通信. 也可以通过对脚本进行编程,使其在收到特定信号时执行 ...
- Linux Shell脚本编程-函数
函数介绍 定义:把一段独立功能的的代码当做一个整体,并为之一个名字,命名的代码段,此即为函数: 功能:函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程. 注意: ...
随机推荐
- Matlab 中的varargin/nargin varargout/nargout
Varargin = var+ arg+ in = variable length(可变长) input argument(输入参数) list(列表) :允许调用该函数时根据需要改变输入参数的个数 ...
- mysql表、函数等被锁住无响应的问题
场景: 在对表或函数等进行操作的时候,如果出现无法响应的情况(排除外网的网络问题),此时极有可能被某一个线程锁定了(这是函数的情况,表的话可能是被某一个用户锁定了),锁定的原因一般都是死循环出不来,而 ...
- 【EF Core】Entity Framework Core 批处理语句
在Entity Framework Core (EF Core)有许多新的功能,最令人期待的功能之一就是批处理语句.那么批处理语句是什么呢?批处理语句意味着它不会为每个插入/更新/删除语句发送单独的请 ...
- BZOJ 1834 网络扩容(最大流+费用流)
对于第一问,直接求最大流. 对于第二问,建源点s和汇点t,s连1容量为INF,费用为0的边,n连t容量为最大流+k,费用为0的边.这样就把最大流限制为最多增加k了. 限制需要求扩充的最小费用,原图的边 ...
- 【以前的空间】bzoj [ZJOI2007]棋盘制作
具体可以去跪<浅谈用极大化思想解决最大子矩形问题>(p.s. 蒟蒻跪了还是很晕,不过想到之前usaco好像是最后一章的一道题……看了下代码顿然醒悟) 也就是如果用o(nm)的方法维护一个极 ...
- [Leetcode] gas station 气站
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You ...
- redux connect的浅比较说明
redux的connect方法是一个高阶组件,对包装的组件会在ShouldComponentUpdate中实现一个默认的浅比较. connect形式如下: connect([mapStateToPro ...
- SRM12 T2夏令营(分治优化DP+主席树 (已更新NKlogN)/ 线段树优化DP)
先写出朴素的DP方程f[i][j]=f[k][j-1]+h[k+1][i] {k<i}(h表示[k+1,j]有几个不同的数) 显然时间空间复杂度都无法承受 仔细想想可以发现对于一个点 i ...
- NOIP2016Day1T2天天爱跑步(LCA+桶)
据说是今年NOIP最难一题了...我还记得当时满怀期待心情点开Day1的题发现T2就不会了于是怀疑人生良久... 啊好像很多大爷都是用线段树合并写的,我怎么什么数据结构都不会啊呜呜呜... 题目大意就 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...