【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命令组成的语句块,实现代码重用和模块化编程. 注意: ...
随机推荐
- Expected Conditions的常用函数
Expected Conditions的使用场景有两种 1.直接在断言中使用 2.与WebDriverWait配合使用,动态等待页面上元素出现或者消失 1. title_is: 判断当前页面的ti ...
- 高性能python
参考来源:Python金融大数据分析第八章 提高性能有如下方法 1.Cython,用于合并python和c语言静态编译泛型 2.IPython.parallel,用于在本地或者集群上并行执行代码 3. ...
- Maven pom 文件解释
1 - 什么是构建? 我们都知道,写完代码之后需要进行编译和运行,以笔者自身为例,使用 Eclipse 写完代码,需要进行编译,再生成 war 包,以便部署到 Tomcat. 在编写 Java 代码的 ...
- 【Python】Python中的引用和赋值
本文转自:http://my.oschina.net/leejun2005/blog/145911 在 python 中赋值语句总是建立对象的引用值,而不是复制对象.因此,python 变量更像是指针 ...
- bzoj 4568 [SCOI 2016] 幸运数字
题目大意 给定一棵\(n\)个点的树,每个点有权值 \(q\)次询问树上路径中 每个点权值可选可不选的最大异或和 \(n\le 2*10^4,q\le 2*10^5,val[i]\le 2^{60}\ ...
- OpenCV2.3.1在Win7+VS2010下的配置过程
1. 假定电脑上已经安装了VS2010程序,若没有,首先安装vs2010.下载OpenCV2.3.1,网址:http://sourceforge.net/projects/opencvlibrary ...
- Codeforces Round #342 (Div. 2) C
C. K-special Tables time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- im-switch -s ibus错误:Error: no configuration file "ibus" exists.
在虚拟机上安装Ubuntu14.04 后安装ibus输入法,万万没想到在切换输入法的时候居然出错了! 无语了,再网上查了一下,这个错误出现的还是比较少的. 先说Ubuntu输入法(ibus)安装的一般 ...
- Qt ------ 初始化构造函数参数,parent
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...
- Moodle配置
Moodle配置 1. 内部设置 在 Moodle 站点管理员界面中有一系列的配置页面(可以从'设置' 块中访问 '网站管理'区).这里有一些重要的系统设置,你需要进行检查. 根据提示信息并结合实 ...