10-4.shell编程.ppt_第1页
10-4.shell编程.ppt_第2页
10-4.shell编程.ppt_第3页
10-4.shell编程.ppt_第4页
10-4.shell编程.ppt_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、$*。,#!/bin/bashfunction fun1 echo this is the first functionfun1fun2() echo this is the second functionfun2 #bash test.sh this is the first functionthis is the second function,必须先定义,再调用,#!/bin/bashfunction fun1 echo this is the first functionfun1fun2fun2() echo this is the second function,函数名必须唯一,#!

3、/bin/bashfunction fun1 echo this is the first functionfun1fun1() echo this is the second functionfun1#bash test.sh this is the first functionthis is the second function,#!/bin/bash function hello () echo Hello,$1 today is date echo now going to the function hello hello kmust echo back from the funct

4、ion,#!/bin/bashfunction fun1 echo this is the first function ls -l xxfun1echo the exit status is :$? #bash test.sh this is the first functionls: xx: 没有那个文件或目录the exit status is :2 函数的退出状态为2,因为最后一条命令执行出错,#!/bin/bashfunction fun1 ls -l xx echo this is the first functionfun1echo the exit status is :$?

5、#bash test.sh ls: xx: 没有那个文件或目录this is the first functionthe exit status is :0 退出状态值却是0 ,因为最后一条命令执行无错,return命令可以使用单个整数值来定义函数退出状态,#!/bin/bashfunction fun1 read -p enter a value: value echo doubling the value return $ $value * 2 fun1echo the new value is $? #bash test.sh enter a value:24doubling the v

6、aluethe new value is 48,#!/bin/bashfname () read -p “please input a value:” valueecho $ $value * 2 result=fname#反引号echo the result is : $result#bash test.sh please input a value:13the result is : 26,函数参数,#!/bin/bashfunction fname if $# -eq 0 | $# -gt 2 then echo -1elif $# -eq 1 then echo $ $1 + $1 e

7、lse echo $ $1 * $2 fi,echo #1#echo -n adding 10 and 15 :value= fname 10 15echo $valueecho #2#echo -n adding just one num of 10:value= fname 10echo $valueecho #3#echo -n adding no num:value= fname echo $valueecho #4#echo -n adding three num 10 20 30 :value= fname 10 20 30echo $value,# bash test.sh #1

8、#adding 10 and 15 :150#2#adding just one num of 10:20#3#adding no num:-1#4#adding three num 10 20 30 :-1,#!/bin/bashfname () echo $ $1 + $2 if $# -eq 2 then value=fname #value=bad $1 $2 echo the result is : $valueelse echo please input two parametersfi #bash test.sh please input two parameters#bash

9、test.sh 1 2,全局变量,#!/bin/bashfun () value=$ $value * 2 read -p please input the value: value fun echo the new value is : $value,local-局部变量,#!/bin/bashfunction fun local temp=$ $value + 5 result=$ $temp + 2 temp=8value=10 fun echo the result is $resultif $temp -gt $value thenecho temp is largerelseech

10、o temp is smallerfi,创建库,funLib.sh function fun1 echo $ $1 + $2 function fun2 echo $ $1 * $2 test2.sh #!/bin/bash./funLib.shresult=fun1 12 24echo the result is $result,select,select 表达式是一种bash的扩展应用,尤其擅长于交互式使用。用户可以从一组不同的值中进行选择。 select var in . ; do break done,#!/bin/sh echo What is your favourite OS?

11、select var in Linux Gnu Hurd Free BSD Other; do break done echo You have selected $var,下面是该脚本运行的结果: What is your favourite OS? 1) Linux 2) Gnu Hurd 3) Free BSD 4) Other #1 You have selected Linux,#!/bin/bash ftype=file $1 case $ftype in $1: Zip archive*) unzip $1 ; $1: gzip compressed*) gunzip $1 ; $1: bzip2 compressed*) bunzip2 $1 ; *) error File $1 can not be uncompressed with smartzip; esac,#!/bin/bashPrintHelp() echo FORMAT: command arg echo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论