10-4shell编程.ppt

上传人:qwe****56 文档编号:70010111 上传时间:2023-01-14 格式:PPT 页数:24 大小:418KB
返回 下载 相关 举报
10-4shell编程.ppt_第1页
第1页 / 共24页
10-4shell编程.ppt_第2页
第2页 / 共24页
点击查看更多>>
资源描述

《10-4shell编程.ppt》由会员分享,可在线阅读,更多相关《10-4shell编程.ppt(24页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Linux Shell编程编程函数函数函数定义在shell中还可以定义函数。函数实际上也是由若干条shell命令组成的,因此它与shell程序形式上是相似的,不同的是它不是一个单独的进程,而是shell程序的一部分。函数定义的基本格式为:functionfnameorfname若干命令行调用函数的格式为:fnameparam1param2shell函数可以完成某些例行的工作,而且还可以有自己的退出状态,因此函数也可以作为if、while等控制结构的条件。在函数定义时不用带参数说明,但在调用函数时可以带有参数,此时shell将把这些参数分别赋予相应的位置参数$1、$2、.及$*。#!/bin/b

2、ashfunctionfun1echothisisthefirstfunctionfun1fun2()echothisisthesecondfunctionfun2#bashtest.shthisisthefirstfunctionthisisthesecondfunction必须先定义,再调用#!/bin/bashfunctionfun1echothisisthefirstfunctionfun1fun2fun2()echothisisthesecondfunction函数名必须唯一#!/bin/bashfunctionfun1echothisisthefirstfunctionfun1fu

3、n1()echothisisthesecondfunctionfun1#bashtest.shthisisthefirstfunctionthisisthesecondfunction#!/bin/bashfunctionhello()echoHello,$1todayisdateechonowgoingtothefunctionhellohellokmustechobackfromthefunction#!/bin/bashfunctionfun1echothisisthefirstfunctionls-lxxfun1echotheexitstatusis:$?#bashtest.shthi

4、sisthefirstfunctionls:xx:没有那个文件或目录theexitstatusis:2函数的退出状态为2,因为最后一条命令执行出错#!/bin/bashfunctionfun1ls-lxxechothisisthefirstfunctionfun1echotheexitstatusis:$?#bashtest.shls:xx:没有那个文件或目录thisisthefirstfunctiontheexitstatusis:0退出状态值却是0,因为最后一条命令执行无错return命令可以使用单个整数值来定义函数退出状态#!/bin/bashfunctionfun1read-pente

5、ravalue:valueechodoublingthevaluereturn$value*2fun1echothenewvalueis$?#bashtest.shenteravalue:24doublingthevaluethenewvalueis48#!/bin/bashfname()read-p“pleaseinputavalue:”valueecho$value*2result=fname#反引号echotheresultis:$result#bashtest.shpleaseinputavalue:13theresultis:26函数参数#!/bin/bashfunctionfnam

6、eif$#-eq0|$#-gt2thenecho-1elif$#-eq1thenecho$1+$1elseecho$1*$2fiecho#1#echo-nadding10and15:value=fname1015echo$valueecho#2#echo-naddingjustonenumof10:value=fname10echo$valueecho#3#echo-naddingnonum:value=fnameecho$valueecho#4#echo-naddingthreenum102030:value=fname102030echo$value#bashtest.sh#1#addin

7、g10and15:150#2#addingjustonenumof10:20#3#addingnonum:-1#4#addingthreenum102030:-1#!/bin/bashfname()echo$1+$2if$#-eq2thenvalue=fname#value=bad$1$2echotheresultis:$valueelseechopleaseinputtwoparametersfi#bashtest.shpleaseinputtwoparameters#bashtest.sh12全局变量#!/bin/bashfun()value=$value*2read-ppleaseinp

8、utthevalue:valuefunechothenewvalueis:$valuelocal-局部变量#!/bin/bashfunctionfunlocaltemp=$value+5result=$temp+2temp=8value=10funechotheresultis$resultif$temp-gt$valuethenechotempislargerelseechotempissmallerfi创建库funLib.shfunctionfun1echo$1+$2functionfun2echo$1*$2test2.sh#!/bin/bash./funLib.shresult=fun1

9、1224echotheresultis$resultselectselect表达式是一种bash的扩展应用,尤其擅长于交互式使用。用户可以从一组不同的值中进行选择。selectvarin.;dobreakdone#!/bin/shechoWhatisyourfavouriteOS?selectvarinLinuxGnuHurdFreeBSDOther;dobreakdoneechoYouhaveselected$var下面是该脚本运行的结果:WhatisyourfavouriteOS?1)Linux2)GnuHurd3)FreeBSD4)Other#1YouhaveselectedLinux#

10、!/bin/bashftype=file$1case$ftypein$1:Ziparchive*)unzip$1;$1:gzipcompressed*)gunzip$1;$1:bzip2compressed*)bunzip2$1;*)errorFile$1cannotbeuncompressedwithsmartzip;esac#!/bin/bashPrintHelp()echoFORMAT:commandargechoNOTE:theargmustbenumberechosuchas:command100000returnif$#-lt1;thenPrintHelpexit-1fiif$1=*!0-9*;thenechotheargmustbenumberexit-1fi作业学习函数的创建及使用方法

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com