shell函数

摘要:

定义函数、函数文件、函数调用、载入和删除函数、参数传递、函数返回状态


6.1函数定义

shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数


定义函数的格式为:

函数名 ()

{

命令1

。。

}

function 函数名 ()

{

。。。

}

函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中。

<pre name="code" class="plain">#!/bin/bash
#hellofun
hello ()
{
    echo "hello,today is `date`"
    return 1
}

 

函数的调用

#!/bin/bash
#hellofun
hello ()
{
    echo "hello,today is `date`"
}
echo "now going to the function hello"
hello
echo "back from the function"


6.2参数传递

向参数传递参数就像在脚本中使用位置变量$1,$2,...$9

 #!/bin/bash
 #hellofun
 hello ()
 {
         echo "hello,$1 today is `date`"
 }
 echo "now going to the function hello"
 hello cyf
 echo "back from the function"


6.3函数文件

  1 #!/bin/bash
  2 #cyf
  3 #source function
  4 . hellofun
  5 echo "now going to the function hello"
  6 hello
  7 echo "back from the function "

  1 #!/bin/bash
  2 #hellofun
  3 hello ()
  4 {
  5     echo "hello,today is `date`"
  6     return 1
  7 }


6.4检查载入函数和删除函数

查看载入函数

-set

删除函数

unset


6.6函数返回状态值

  1 #!/bin/bash
  2 #hellofun
  3 hello ()
  4 {
  5     echo "hello,today is `date`"
  6     return 1
  7 }

  1 #!/bin/bash
  2 #cyf
  3 #source function
  4 . hellofun
  5 set
  6 echo "now going to the function hello"
  7 hello
  8 echo $?
  9 echo "back from the function "





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值