shell简明教程3函数,你将学习如何创建函数


3 函数

在本章中,您将了解为什么以及何时需要使用函数。 你将学习如何创建函数以及如何使用函数。 我们将讨论变量及其作用域。 学习如何使用参数访问传递给函数的参数。 最后,您还将学习如何使用函数处理退出状态和返回代码。

计算机编程和应用程序开发中有一个概念叫做 DRY。 DRY 是 "不要重复"(Don't Repeat Yourself)的缩写。 通过函数,您只需编写一次代码块,即可多次使用。每次需要执行特定任务或功能时,只需调用包含该代码的函数,而无需重复几行代码。这有助于缩短脚本的长度,还能让您在一个地方对给定任务进行更改、测试、故障排除和记录。所有这些都使脚本更易于维护。

每当您需要在脚本中多次执行同一操作时,这就表明您可能应该为该操作编写函数。 函数只是可重复使用的代码块,它执行操作并返回退出状态或返回代码。 函数在调用前必须先定义。 调用函数时,可以向函数传递数据。 您可以在函数中以参数的形式访问这些数据。

3.1 创建函数

创建函数有两种方法。 第一种方法是明确使用关键字function,然后在关键字后面加上函数名称和括号。 然后使用开头的大括号。 当函数被调用时,后面的代码或命令将被执行。要结束函数,请使用结尾大括号。

function function-name() {
    # Code goes here.
}

声明函数的第二种方法与第一种完全相同,只是在声明中不使用关键字 unction。 其他一切保持不变。

function-name() {
    # Code goes here.
}

3.2 调用函数

要调用函数,只需在脚本中列出函数名称即可。 调用函数时,不要使用括号。 你可能在其他编程语言中见过这种语法和样式,但在shell脚本中不起作用。 只需将函数名称放在一行中,它就会执行该函数。
运行此脚本时,屏幕上会显示 "Hello!"。

#!/bin/bash
function hello() {
    echo "Hello!"
}
hello

执行:

$ ./functions-01.sh 
Hello!

函数可以调用其他函数:

#!/bin/bash

function hello() {
    echo "Hello!"
    now
}

function now() {
    echo "It's $(date +%r)"
}

hello

执行:

Hello!
It's 下午 08时42分21秒

函数必须在使用前声明,不要这样做:

#!/bin/bash

function hello() {
    echo "Hello!"
    now
}

# This will cause an error as the "now()" function is not yet defined.
hello

function now() {
    echo "It's $(date +%r)"
}

执行:

$ ./functions-03.sh 
Hello!
./functions-03.sh: line 5: now: command not found

脚本语言不是预编译的。 在某些语言中,你可以在任何地方定义函数,编译器会检查所有源代码,然后将它们拼凑在一起,最终执行程序。 在脚本中,命令和组件会在运行时从上到下读取。最好将所有函数放在脚本的顶部。 这样可以确保在使用前定义好所有函数。

3.3 位置参数

使用$1、2等参数访问这些传入参数的值。 唯一不同的是,$0仍然是脚本本身的名称。您不能使用 $0 访问函数的名称,但好消息是,无论如何您都不会真的想这样做。
要向函数发送数据,请在函数名称后输入数据。 在本例中,hello 函数的调用只有一个参数,即 Jason 。 这意味着 hello 函数中 $1 的内容是 "Jason"。 正如你所猜测的,这个脚本的输出就是 "Hello Jason"。

#!/bin/bash

function hello() {
    echo "Hello $1"
}

hello Jason

执行:

$ ./functions-04.sh 
Hello Jason

下面脚本将循环处理传递给hello函数的每个参数:

#!/bin/bash

function hello() {
    for NAME in $@
    do
        echo "Hello $NAME"
    done
}

hello Jason Dan Ryan

执行:

$ ./functions-05.sh
Hello Jason
Hello Dan
Hello Ryan

参考资料

  • 软件测试精品书籍文档下载持续更新 https://github.com/china-testing/python-testing-examples 请点赞,谢谢!
  • 本文涉及的python测试开发库 谢谢点赞! https://github.com/china-testing/python_cn_resouce
  • python精品书籍下载 https://github.com/china-testing/python_cn_resouce/blob/main/python_good_books.md
  • Linux精品书籍下载 https://www.cnblogs.com/testing-/p/17438558.html

3.4 变量作用域

默认情况下,所有变量都是全局变量。 这意味着变量及其值可以在脚本的任何地方访问,包括在任何函数中。在使用变量之前,必须先定义变量。

#!/bin/bash

my_function() {
    echo "$GLOBAL_VAR"
}

GLOBAL_VAR=1
# The value of GLOBAL_VAR is available to my_function
my_function

执行:

$ ./functions-06.sh
1

如果在函数中定义了全局变量,那么在函数被调用和执行之前,它在函数之外是不可用的。

#!/bin/bash

my_function() {
    echo "$GLOBAL_VAR"
}

# The value of GLOBAL_VAR is NOT available to my_function since GLOBAL_VAR was defined after my_function was called.
my_function
GLOBAL_VAR=1

执行:

 ./functions-07.sh

#!/bin/bash

my_function() {
    GLOBAL_VAR=1
}

# GLOBAL_VAR not available yet.
echo "GLOBAL_VAR value BEFORE my_function called: $GLOBAL_VAR"

my_function

# GLOBAL_VAR is NOW available
echo "GLOBAL_VAR value AFTER my_function called: $GLOBAL_VAR"

执行:

$ ./functions-08.sh
GLOBAL_VAR value BEFORE my_function called: 
GLOBAL_VAR value AFTER my_function called: 1

3.5 局部变量

局部变量是指只能在声明它的函数中访问的变量。 在变量名前使用local关键字。 只有在首次使用局部变量时才使用local关键字。最佳做法是在函数内部使用局部变量。

#!/bin/bash

my_function() {
    local LOCAL_VAR=1
    echo "LOCAL_VAR can be accessed inside of the function: $LOCAL_VAR"
}

my_function

# LOCAL_VAR is not available outside of the function.
echo "LOCAL_VAR can NOT be accessed outside of the function: $LOCAL_VAR"

执行:

 ./functions-09.sh
LOCAL_VAR can be accessed inside of the function: 1
LOCAL_VAR can NOT be accessed outside of the function: 

3.6 退出状态和返回代码

函数也有退出状态,有时也称为返回代码。 可以通过使用return语句明确设置退出状态,并在后面加上希望返回的状态。 如果不使用返回语句,函数的退出状态就是函数中最后执行的命令的退出状态。
返回语句只接受数字。 只有介于0和255之间的整数才能用作退出状态。 退出状态为0表示命令或函数成功完成。非0的退出状态表示某种类型的错误。要访问函数的退出状态,请在调用函数后立即使用$?。
在本代码段中,$?的值将是 my_function 函数的退出状态。

my_function
echo $?

实例:backup_file函数将创建文件的备份,并将其放入/var/tmp目录。

#!/bin/bash

function backup_file () {
  # This function creates a backup of a file.

  # Make sure the file exists.
  if [ -f "$1" ] 
  then
    # Make the BACKUP_FILE variable local to this function.
    local BACKUP_FILE="/tmp/$(basename ${1}).$(date +%F).$$"
    echo "Backing up $1 to ${BACKUP_FILE}"

    # The exit status of the function will be the exit status of the cp command.
    cp $1 $BACKUP_FILE
  else
    # The file does not exist, so return an non-zero exit status.
    return 1
  fi
}

# Call the function
backup_file /etc/hosts

# Make a decision based on the exit status of the function.
# Note this is for demonstration purposes.  You could have
# put this functionality inside of the backup_file function.
if [ $? -eq "0" ]
then
  echo "Backup succeeded!"
else
  echo "Backup failed!"
  # Abort the script and return a non-zero exit status.
  exit 1
fi

执行:

$ ./functions-10.sh
Backing up /etc/hosts to /tmp/hosts.2023-08-26.11232
Backup succeeded!
钉钉或微信号: pythontesting 微信公众号:pythontesting

相关内容