shell函数脚本的调用

函数脚本的调用.
root@ptubuntu:/lib/lsb# vi init-functions

察看函数 set

设置hello设置调用.

root@ptubuntu:~# vi func
!/bin/bash
#func
. fun
set   //设置查看函数
echo “now going to the unction hello ”
hello
echo “back from the function”

查看函数hello是否被调用.

root@ptubuntu:~# ./func    //
TERM=xterm
UID=0
USER=root
XDG_SESSION_COOKIE=20ffd3439e326b98eada2fc9492fb7e6-1228894566.802095-1306568828
_=fun
hello ()
{
echo “Hello,taday is `date`”;     //这段就可以看到正确的函数数被调用出来.
return 0
}

now going to the unction hello
Hello,taday is Wed Dec 10 03:24:17 EST 2008
back from the function

删除函数 unset

#!/bin/bash
#func
. fun
unset hello  //设置土删除hello函数
echo “now going to the unction hello ”
hello
echo “back from the function”

查看函数hello是否被调用.

root@ptubuntu:~# ./func
now going to the unction hello
./func: line 6: hello: command not found    // 在这里报错.没有找到相关的hello函数
back from the function

正确例子hello函数的调用.

root@ptubuntu:~# vi fun //设置hello函数
#!/bin/bash
#hellfunctions
function hello ()
{
echo “Hello,taday is `date`”
return 0   //返回状态值为0是一般为正确值.
}

root@ptubuntu:~# vi function   //程序调用hello函数
#!/bin/bash
#func
. fun //在这里指到上面要调用到函数的库名fun
echo “now oging to the function hello”
hello
echo $?
echo “back from the function”

输入的结果:

root@ptubuntu:~# ./function
now oging to the function hello
Hello,taday is Wed Dec 10 03:15:38 EST 2008
0
back from the function

原创文章,转载请注明: 转载自PT Ubuntu Blog

本文链接地址: http://www.ptubuntu.com/2008/12/865.html

Share

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Verify Code   If you cannot see the CheckCode image,please refresh the page again!