第六章 shell函数
1.定义函数
1funcation name()
2{
3 command1
4 ....
5}
6或
7函数名()
8 {
9 command1
10 ...
11 }
12eg.
13#!/bin/bash
14#hellofun
15function hello()
16{
17echo "hello,today is `date`"
18return 1
19}
2.函数调用
1#!/bin/bash
2#hellofun
3function hello()
4{
5echo "hello,today is `date`"
6return 1
7}
8echo "now going to the function hello"
9hello
10echo "back from the function"
所以调用函数只需要在脚本中使用函数名就可以了。
3.参数传递
像函数传递参数就像在脚本中使用位置变量$1,$2…$9
4.函数文件
函数可以文件保存。在调用时使用”. 函数文件名”(.+空格+函数文件名)
1如:
2hellofun.sh
3#!/bin/bash
4#hellofun
5function hello()
6{
7echo "hello,today is `date`"
8return 1
9}
10func.sh
11#!/bin/bash
12#func
13. hellofun.sh
14echo "now going to the function hello"
15echo "Enter yourname:"
16read name
17hello $name
18echo "back from the function"
19[test@szbirdora 1]$ sh func.sh
20now going to the function hello
21Enter yourname:
22hh
23hello,hh today is Thu Mar 6 15:59:38 CST 2008
24back from the function
5.检查载入函数 set
删除载入函数 unset 函数名
6.函数返回状态值
return 0、return 1
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/function/231.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.