Shell Script函数
来源:互联网 发布:ubuntu google输入法 编辑:程序博客网 时间:2024/06/05 08:00
函数
在大多数编程语言中,都会存在函数的概念,如C语言中的printf()
。在编程过程中,将一段会重复使用且为完成特定功能的语句写在函数中,可以减少代码的长度提高可读性。在ShellScript中,除了使用函数外有另一种选择,即将一个较长的脚本分为多个小脚本文件,但这种方法的效率会比使用函数低,在实际编写中可根据需要选择使用函数或者分文件。
在ShellScript中函数的定义与使用的方法也非常简单。定义一个函数只需要写出函数名,再跟上一对圆括号和花括号,并将具体的语句写在花括号中;使用函数时只需写出函数名即可调用该函数。
# 函数的定义function_name() { statements}# 使用函数function_name
下面使用一个简单的例子演示函数的使用。
#!/bin/shfoo() { echo "Function foo is executing"}echo "script starting"fooecho "script ended"
程序先是定义(不执行)了函数foo,然后开始执行下面的代码。执行完echo "script starting
后执行foo
将调用函数foo,于是程序将执行函数foo中的语句。执行该程序,将得到以下结果。
script startingFunction foo is executingscript ended
函数参数与返回值
在函数的使用中经常需要传递参数和返回值。在定义函数时,无需像其它语言一样预先定义函数的个数及名称,而是在函数中直接使用参数变量即可,参数变量参见Shell Script介绍及变量使用。在调用函数时,若需要传递参数,只需在函数名后加上参数,如function_name arg1 arg2 ...
。函数的值返回方法与其它大部分语言类似,即return return_value
。下面将使用一个小程序演示函数参数及返回值的使用。
#!/bin/shyes_or_no() { echo "Is your name $* ?" while true do echo -n "Enter yes or no: " read x case "$x" in [yY] | [yY][eE][sS] ) return 0 ;; [nN] | [nN][oO] ) return 1 ;; * ) echo "answer yes or no" ;; esac done}if yes_or_no "xxx"then echo "Hi xxx, nice name"else echo "Never mind"fiexit 0
下面将执行两次程序,分别输入yes与no,结果如下。
# first timeIs your name xxx ?Enter yes or no: yesHi xxx, nice name# second timeIs your name xxx ?Enter yes or no: noNever mind
0 0
- Shell Script函数
- 【Unix/Linux.Shell Script】函数
- linux shell script中的函数简介
- Linux重新学习--shell script--函数
- shell Script
- shell script
- Shell script
- shell script
- shell script
- shell script
- shell script
- shell script
- shell script
- shell script
- shell script
- Shell Script
- shell script
- shell script
- http请求学习
- Javascript的Date类型总结
- Spark--用Java开发微服务
- 51 nod 数组的最大代价
- MapReduce计数器
- Shell Script函数
- 计数排序
- linux 监控线程命令
- 基于KNN算法的约会网站配对效果 python3.2
- HDU 1035 模拟
- post 提交 和 get 提交的区别 文件上传
- Light OJ 1008 Fibsieve`s Fantabulous Birthday(找出数字位置)
- java实现递归快排和非递归快排
- SpringMVC中的java.lang.ClassNotFoundException: org.aspectj.weaver.BCException 调试过程记录