Linux程序设计(Linux shell编程八)
来源:互联网 发布:java实验指导书答案 编辑:程序博客网 时间:2024/04/23 22:21
各位看官,上一回咱们说了Linux shell编程中的函数,不过只是看了一个函数的形式,今天咱们接着说
函数的其它内容。
函数有三个重要的部分:函数调用,函数返回值,函数参数。下面我们分别来说这三个部分的内容。
函数调用:所谓的函数调用,就是在程序的某个地方使用函数来实现功能,就像看官在这里叫小二给你泡
茶一样。只需要在程序中使用函数名,就能调用函数。不过,使用函数名时,需要先定义函数,不然程序
不知道去哪个找这个函数。函数调用的例子暂时先不举。待会用其它例子来说明。
函数返回值:函数的返回值准确地来说是函数的退出状态值。咱们先来说说什么是退出状态值。在Linux
中每个命令执行完成后,都会返回一个退出状态值,用来表示命令执行的结果。如果命令执行成功,命令
的退出状态值为0,否则为非0。在Linux中有一个变量用来存储命令的退出状态值,这个变量是”?“变量。
而函数的返回值默认为函数中最后一个命令的退出状态值。下面咱们举例子来说明。
#! /bin/bashecho "-----------------the starting line of shell-----------------"cat hello.shecho "the value of "?" is:$?"echo "hello"echo "the value of "?" is:$?"echo "show the value of function returned"func1(){ cat hello.sh}echo "call func1"func1echo "the value of function returned is:$?"func2(){ echo " hello "}echo "call func2"func2echo "the value of function returned is:$?"echo "-----------------the ending line of shell-----------------"
新建立一个名叫t1.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在
终端中运行该文件,并用依据程序提示输入内容得到以下结果:
-----------------the starting line of shell-----------------
cat: hello.sh: No such file or directory
the value of ? is:1
hello
the value of ? is:0
show the value of function returned
call func1
cat: hello.sh: No such file or directory
the value of function returned is:1
call func2
hello
the value of function returned is:0
-----------------the ending line of shell-----------------
看官们,上面的例子中先是使用cat命令打开hello.sh文件,结果提示没有该文件,说明cat命令执行失
败,这时我们可以看到命令的返回值为1.接着使用echo命令输入hello。这时我们可以看到命令的返回值
为0,说明echo命令执行成功。这一小段主要是用来说明命令的退出状态值与命令执行结果之间的关系。下
面的程序中定义了两个函数:func1和func2。这两个函数没有实现什么特殊的功能,只是把程序上面的
cat和echo命令放到的函数的大括号中。看官们可以看到,程序的返回值和上面cat与echo命令的退出状
态值完全一样。这说明函数的返回值,就是函数中最后一个命令执行后的退出状态值。看官们,刚才咱们
说函数调用时没有举例子,现在大家看上面的例子中有两行代码fun1和fun2。从例子中可以看到,在程
序中直接使用函数名,就是在进行函数调用,或者叫调用函数。
看官们咱们刚才说的是函数默认的返回值,其实也可以手动指定函数的返回值。这时需要通过return命令
来实现。下面举例子来说明,如何手动指定函数返回值。
#! /bin/bashecho "-----------------the starting line of shell-----------------"func1(){ if `cat hello.sh` then return 3 else return 5 fi}echo "call func1"func1echo "the value of function returned is:$?"echo "-----------------the ending line of shell-----------------"
新建立一个名叫t2.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在
终端中运行该文件,并用依据程序提示输入内容得到以下结果:
-----------------the starting line of shell-----------------
call func1
cat: hello.sh: No such file or directory
the value of function returned is:5
-----------------the ending line of shell-----------------
看官们,上面的例子中定义了一个叫func1的函数,在函数中判断cat命令的执行结果,如果cat命令执行
成功,func1函数返回3,否则返回5.大家从程序运行结果中可以看到,cat命令找不到hello.sh文件,执
行失败。所以函数返回值为5.通过这个例子,大家应该明白如何手动指定函数的返回值。通常情况下,我们
写shell程序的时候,最好手动指定函数的返回值。另外,默认的程序返回值与手动指定的程序返回值还
有区别:默认的函数返回值介于0-256之间,而手动指定的函数返回值没有这个限制。
各位看官,关于函数的内容,咱们今天就说到这里,欲知后事如何,且听下回分解。
- Linux程序设计(Linux shell编程八)
- Linux程序设计(Linux shell编程一)
- Linux程序设计(Linux shell编程二)
- Linux程序设计(Linux shell编程三)
- Linux程序设计(Linux shell编程四)
- Linux程序设计(Linux shell编程五)
- Linux程序设计(Linux shell编程六)
- Linux程序设计(Linux shell编程七)
- Linux程序设计(Linux shell编程九)
- Linux程序设计(Linux shell编程十)
- Linux程序设计(Linux shell编程十一)
- Linux程序设计(Linux shell编程十二)
- Linux程序设计(Linux shell编程十三)
- Linux程序设计(Linux shell编程十四)
- Linux学习(八):shell脚本编程
- Linux程序设计(Linux shell编程的例子:倒计时程序)
- Linux程序设计(Linux shell编程的例子:选择菜单)
- Linux程序设计(Linux shell编程的例子:总结)
- 搜索引擎倒排索引表压缩:gamma编码
- Linux显示邮件状态等信息
- 递归四条基本法则
- #include <sys/types.h>的作用
- HDU 3631 Shortest Path (Floyd的深层理解)
- Linux程序设计(Linux shell编程八)
- android中 camera 拍照应用详解
- Android基础笔记(三)-数据存储和界面展现
- 1016. Phone Bills (25)
- HTTP 协议之压缩
- 没收心的人是需要别人适当地提醒的
- 东半球最好的SecureCRT高级教程
- HDU 1009 贪心水题
- 求十个数中最大的数