Section 14.1: 带参数的函数

helloJohn.sh 的内容清单如下:

#!/bin/bash

greet() {
    local name="$1"
    echo "Hello, $name"
}

greet "John Doe"

# running above script
$ bash helloJohn.sh
Hello, John Doe
  1. 如果不以任何方式修改参数,则无需将其复制到局部变量中,只需 echo “Hello, $1” 即可。

  2. 您可以使用 $1、$2、$3 等来访问函数内部的参数。

注意:您如果需要使用${10}、${11} 等参数,也就是超过 9 个参数的时候,$10 将不起作用(bash 会将其读作1 0),。

  1. $@ 指函数的所有参数:
#!/bin/bash
foo() {
        echo "$@"
}
foo 1 2 3 # output => 1 2 3

注意:实际上,你应该总是在 $@ 周围使用双引号",就像这里一样。

省略双引号会导致 shell 扩展通配符(即使用户为了避免这种情况而特意加了双引号),通常会引入不受欢迎的行为,甚至可能带来安全问题。

foo "string with spaces;" '$HOME' "*"
# output => string with spaces; $HOME *
  1. 默认参数使用 ${1:-default_val}。例如
#!/bin/bash
foo() {
    local val=${1:-25}
    echo "$val"
}

foo # output => 25
foo 30 # output => 30
  1. 使用 ${var:?error message} 来要求一个参数
foo() {
    local val=${1:?Must provide an argument}
    echo "$val"
}

Section 14.2: 简单函数

In helloWorld.sh

#!/bin/bash

# Define a function greet
greet ()
{
        echo "Hello World!"
}

# Call the function greet
greet

在运行脚本时,我们会看到以下信息

$ bash helloWorld.sh
Hello World!

需要注意的是,如果文件中包含函数,则当前 bash 会话中就可以使用这些函数。

$ source helloWorld.sh # or, more portably, ". helloWorld.sh"
$ greet
Hello World!

您可以在某些 shell 中导出函数,这样子进程就可以看到它。

bash -c 'greet' # fails
export -f greet # export function; note -f
bash -c 'greet' # success

Section 14.3: 处理标志和可选参数

可以在函数内部使用 getopts 内置函数来编写包含标志和可选参数的函数。这并不特别困难,但必须适当处理 getopts 涉及的值。例如,我们定义了一个 failwith 函数,它在 stderr 上写入一条信息,并以代码 1 或作为 -x 选项参数的任意代码退出:

# failwith [-x STATUS] PRINTF-LIKE-ARGV
# Fail with the given diagnostic message
#
# The -x flag can be used to convey a custom exit status, instead of
# the value 1. A newline is automatically added to the output.

failwith()
{
    local OPTIND OPTION OPTARG status

    status=1
    OPTIND=1

    while getopts 'x:' OPTION; do
        case ${OPTION} in
            x) status="${OPTARG}";;
            *) 1>&2 printf 'failwith: %s: Unsupported option.\n' "${OPTION}";;
        esac
    done

    shift $(( OPTIND - 1 ))
    {
        printf 'Failure: '
        printf "$@"
        printf '\n'
    } 1>&2
    exit "${status}"
}

该功能的使用方法如下:

failwith '%s: File not found.' "${filename}"
failwith -x 70 'General internal error.'

请注意,与 printf 一样,变量不应作为第一个参数使用。如果要打印的信息包含一个变量的内容,则应使用 %s 指定符来打印,如

failwith '%s' "${message}"

Section 14.4: 打印函数定义

getfunc() {
        declare -f "$@"
}

function func(){
        echo "I am a sample function"
}

funcd="$(getfunc func)"
getfunc func # or echo "$funcd"

输出:

func ()
{
        echo "I am a sample function"
}

Section 14.5: 接受命名参数的函数

foo() {
    while [[ "$#" -gt 0 ]]
    do
        case $1 in
                -f|--follow)
                local FOLLOW="following"
                ;;
        -t|--tail)
                local TAIL="tail=$2"
                ;;
        esac
        shift
    done

    echo "FOLLOW: $FOLLOW"
    echo "TAIL: $TAIL"
}

使用示例

foo -f
foo -t 10
foo -f --tail 10
foo --follow --tail 10

Section 14.6: 函数的返回值

Bash 中的 return 语句不像 C 函数那样返回值,而是以返回状态退出函数。你可以把它看作函数的退出状态。

如果你想从函数中返回一个值,可以像这样将值发送到 stdout:

fun() {
    local var="Sample value to be returned"
    echo "$var"
    #printf "%s\n" "$var"
}

现在,如果你这样做:

var="$(fun)"

输出结果将存储在 $var 中。

Section 14.7: 函数的退出代码是其最后一条命令的退出代码

下面是一个检查主机是否正常运行的示例函数:

is_alive() {
        ping -c1 "$1" &> /dev/null
}

此函数向第一个函数参数指定的主机发送一次 ping。ping 的输出和错误输出都会重定向到 /dev/null,因此函数不会输出任何内容。但 ping 命令成功时的退出代码为 0,失败时的退出代码非 0。由于这是函数的最后一条(在本例中也是唯一一条)命令,ping 的退出代码将被重用为函数本身的退出代码。

这一用法实际上在条件语句中非常有用。

例如,如果主机 graucho 正常运行,则使用 ssh 连接到它:

if is_alive graucho; then
        ssh graucho
fi

另一个例子:反复检查,直到主机 graucho 启动,然后用 ssh 连接到它:

while ! is_alive graucho; do
        sleep 5
done
ssh graucho