argor

   :: 首页  :: 新随笔  :: 联系 ::  :: 管理

函数定义、调用

$ cat te.sh
#!/bin/bash

# define a function
test()
{
  echo "This is a function."
}

# test function call
test
$ sh te.sh

 

函数库文件

编写函数库文件

#!/bin/bash

# define func
hello()
{
  echo -n "hello $1. "
  credit
}

credit()
{
  echo "Do you need to apply for a credit card ?"
}

载入函数库文件

#!/bin/bash

# call in
. ./lib.sh

hello $1

* 引用函数时不小心带上括号,会报错。

 

函数返回值

函数返回值,由 return 指定。没有使用时,返回值是函数体最后一条命令的返回值。语法:return [n]

 

posted on 2017-11-28 10:38  argor  阅读(188)  评论(0编辑  收藏  举报