linux shell script中的函数简介
来源:互联网 发布:淘宝童装轮播图 编辑:程序博客网 时间:2024/05/18 12:34
懂C语言的人, 没有不知道函数的, 下面我们来看看linux shell script中的函数, 其实也很简单:
[taoge@localhost learn_shell]$ lsa.sh[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashfun(){echo "calling fun"}echo "begin ---"fun # call funecho "end ---"[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh begin ---calling funend ---[taoge@localhost learn_shell]$可以看到, 调用的时候, 不需要写fun(), 而应该用fun, 而且fun函数在定义的时候, 也不需要返回值类型。
再看:
[taoge@localhost learn_shell]$ lsa.sh[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashfun(){echo $1}echo "begin ---"fun # call funecho "end ---"[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh goodbegin ---end ---[taoge@localhost learn_shell]$可以看到, 调用fun的时候, 没有传递参数, 所以fun中实际并不能访问到$1
那行, 我们来传一下参数:
[taoge@localhost learn_shell]$ lsa.sh[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashfun(){echo $0echo $1}echo "begin ---"fun "$1" # call funecho "end ---"[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh begin ---./a.shend ---[taoge@localhost learn_shell]$ ./a.sh goodbegin ---./a.shgoodend ---
可见, 参数传递成功。
最后, 我们来看看return, 注意return 0表示成功:
[taoge@localhost learn_shell]$ lsa.sh[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashfun(){return 0}echo "begin ---"if funthenecho goodelseecho badfiecho "end ---"[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh begin ---goodend ---[taoge@localhost learn_shell]$
linux shell script函数就是这么简单, 你想任性一点, 那也可以。
0 0
- linux shell script中的函数简介
- 【Unix/Linux.Shell Script】函数
- Linux重新学习--shell script--函数
- Shell Script(bash)简介
- Shell Script简介
- Shell Script简介
- 【Unix/Linux.Shell Script】Shell & Shell Script
- Linux程式设计-11.Shell Script(bash)--(1)简介
- linux shell script程序之选择结构简介
- linux shell script程序之循环结构简介
- Shell Script函数
- linux shell中的函数
- LINUX shell script 打包
- linux shell script 入门
- linux shell script 函式
- linux shell script
- Linux shell script
- [linux]shell script 随记
- 将Eclipse4.4修改为传统的界面
- [好程序员训练营]java基础之this和super笔记
- 树状结构显示之递归算法
- Application类应用之全局变量
- Codeforces498C解题报告
- linux shell script中的函数简介
- 软考路——设计模式pk大赛(一)
- C++内联函数
- Oracle中针对中文进行排序
- python datetime模块用strftime 格式化时间
- 于Visual C++ 2010开发Windows7应用-开发多点触控MFC应用程序
- java 多态学习笔记
- Android应用程序UI硬件加速渲染环境初始化过程分析
- iOS 8自定义cell的删除按钮