Shell脚本之:函数
Shell 也支持函数。Shell函数必须先定义后使用。
函数的定义与调用
Shell 函数的定义格式如下:
function_name () {
list of commands
[ return value ]
}
函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。
调用函数只需要给出函数名,不需要加括号。下面给出Shell函数定义和调用的一个简单例子:
#!/bin/bash
# Define your function here
Hello () {
echo "hello world"
}
# Invoke your function
Hello
再来看一个带有return语句的函数:
#!/bin/bash
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"
像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项
函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值
#!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
}
funWithParam
注意,$10不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
Shell脚本之:函数的更多相关文章
- Linux Shell脚本编程-函数
函数介绍 定义:把一段独立功能的的代码当做一个整体,并为之一个名字,命名的代码段,此即为函数: 功能:函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程. 注意: ...
- Shell脚本之八 函数
一.函数定义 Linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; ...
- shell脚本的函数介绍和使用案例
#前言:今天我们来聊聊shell脚本中的函数知识,看一下函数的优势,执行过程和相关的使用案例,我们也来看一下shell和python的函数书写方式有什么不同 #简介 .函数也具有别名类似的功能 .函数 ...
- Shell脚本(五)函数
总结下shell中的函数用法 #!/bin/bash function add_v1() { echo "call function add" } function add_v2( ...
- Shell脚本学习 - 函数,输入输出重定向,文件
函数 函数定义 [ function ] funname [()] { action; [return int;] } 定义时可以是function fun(),也可以直接fun(),不带参数 返回值 ...
- shell脚本之函数的使用
把代码封装成函数,相当于造了一个“轮子”,之后就直接重复使用即可. 函数的创建 shell中函数的创建有2种方式 1.使用function关键字 语法 function test { ... } 2. ...
- shell脚本 4 函数与正则
shell函数 shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直接调用该段代码. 格式 func() { #指定函数名 ...
- shell脚本中${...}函数的用法总结
${...}在字符串中有非常多的用法: 1.${var} 限定变量. 如果一个变量名A是另一个变量名AB的前部分,那么,如果要在AB相连时,要得到A的值,就必须使用${var}限定. 如果变量名不会产 ...
- 在shell脚本中使用函数
转载请标明:http://www.cnblogs.com/winifred-tang94/ 对于在脚本中重复使用的功能模块,可以封装成为函数. shell脚本中函数的定义可以使用如下两种方式: a. ...
- shell脚本函数及数组
函数介绍: 函数function是由若干条shell命令组成的语句块,实现代码重用和模块话编程. 它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部 ...
随机推荐
- 【Tomcat】一台电脑上运行多个tomcat
效果: 1.首先需要安装Tomcat7,Tomcat8. Tomcat7: Tomcat8: 2.添加两个环境变量,添加CATALINA_HOME1和CATALINA_BASE1指向E:\tomcat ...
- BS4爬取豆瓣电影
爬取豆瓣top250部电影 ####创建表: #connect.py from sqlalchemy import create_engine # HOSTNAME='localhost' # POR ...
- 通过OpenGL ES在iOS平台实践增强现实(一)
http://ios.9tech.cn/news/2013/1108/38495.html 1.本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据设 ...
- CentOS下SVN使用
1. 介绍 这里想在CentOS上搭建的是基于http访问的SVN Server 2. 软件 安装相关软件 yum install httpd httpd-devel mod_dav_svn subv ...
- Fiddler抓包10-会话框添加查看get与post请求类型【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 在使用fiddler抓包的时候,查看请求类型get和post每次 ...
- 通过字典传递django orm的filter功能
class AppRightManageListView(ListView): template_name = 'rightmanage/list_apprightmanage.html' # mod ...
- Codeforces Round #445 D. Restoration of string【字符串】
D. Restoration of string time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
- (转)NSString 类的使用
官网连接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/Creat ...
- POJ 3680 Intervals(费用流+负权优化)
[题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...