bash 。返回两个函数级别(两个嵌套调用)

标签 bash nested function-call

我需要知道 Bash 是否有适合我的情况的解决方案。我需要在某些条件下进行“双重返回”。我的意思是,执行一个函数的返回并返回父函数以跳过该父函数的其余代码。

我知道我可以使用函数返回值来实现此目的。但我想知道在 Bash 中是否存在类似“break 2”的功能。如果可能的话,我不想修改父函数的代码,因为您可以想象,在我的真实脚本中,有几十个函数,我不想全部修改。

例子:

#!/bin/bash

function sublevelone() {
    echo "sublevelone"
    # Return 2, or break 2 or something :D
}

function main() {
    sublevelone
    echo "This is the part of the code to being avoid executed"
}

main

最佳答案

我不知道 bash 专家会怎么想,但这至少适用于简单的情况:

multireturn(){
    [ -n "$1" ] && poplevel="$1"
    if [ "$poplevel" -ge 0 ]; then
        trap multireturn DEBUG
        shopt -s extdebug
        (( poplevel-- ))
        return 2
    else
        shopt -u extdebug
        trap - DEBUG
        return 0
    fi
}

这利用了调试陷阱和 extdebug 标志:

extdebug
    If  set  at  shell  invocation,  arrange  to execute the
    debugger profile before the shell starts,  identical  to
    the  --debugger option.  If set after invocation, behav-
    ior intended for use by debuggers is enabled:
    1.     The -F option to the declare builtin displays the
           source file name and line number corresponding to
           each function name supplied as an argument.
    2.     If the command run by the DEBUG  trap  returns  a
           non-zero  value,  the next command is skipped and
           not executed.
    3.     If the command run by the DEBUG  trap  returns  a
           value  of 2, and the shell is executing in a sub-
           routine (a shell function or a shell script  exe-
           cuted  by  the  .  or source builtins), the shell
           simulates a call to return.
    4.     BASH_ARGC and BASH_ARGV are updated as  described
           in their descriptions above.
    5.     Function  tracing  is  enabled: command substitu-
           tion, shell functions, and subshells invoked with
           ( command ) inherit the DEBUG and RETURN traps.
    6.     Error  tracing  is enabled: command substitution,
           shell functions, and  subshells  invoked  with  (
           command ) inherit the ERR trap.

示例用法:

#!/bin/bash

multireturn(){
    [ -n "$1" ] && poplevel="$1"
    if [ "$poplevel" -ge 0 ]; then
        trap multireturn DEBUG
        shopt -s extdebug
        (( poplevel-- ))
        return 2
    else
        shopt -u extdebug
        trap - DEBUG
        return 0
    fi
}

# define 8 levels of function calls
# (level N prints output, calls level N+1, then prints more output)
for i in $(seq 1 8); do
    eval \
'level'$i'(){
    echo -n " '$i'"
    level'$((i+1))'
    echo -n "('$i')"
}'
done

# final level calls multireturn
level9(){
    echo -n " 9"
    multireturn $n
    echo -n "(9)"
}

# test various skip amounts
for i in $(seq 0 10); do
    echo -n "$i:"
    n=$i
    level1
    echo .
done

echo
echo done

结果:

0: 1 2 3 4 5 6 7 8 9(9)(8)(7)(6)(5)(4)(3)(2)(1).
1: 1 2 3 4 5 6 7 8 9(8)(7)(6)(5)(4)(3)(2)(1).
2: 1 2 3 4 5 6 7 8 9(7)(6)(5)(4)(3)(2)(1).
3: 1 2 3 4 5 6 7 8 9(6)(5)(4)(3)(2)(1).
4: 1 2 3 4 5 6 7 8 9(5)(4)(3)(2)(1).
5: 1 2 3 4 5 6 7 8 9(4)(3)(2)(1).
6: 1 2 3 4 5 6 7 8 9(3)(2)(1).
7: 1 2 3 4 5 6 7 8 9(2)(1).
8: 1 2 3 4 5 6 7 8 9(1).
9: 1 2 3 4 5 6 7 8 9.
10: 1 2 3 4 5 6 7 8 9
done

关于 bash 。返回两个函数级别(两个嵌套调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103370/

相关文章:

检查命令是否在 Makefile 中不返回任何内容

javascript - 测试嵌套 JavaScript 对象键是否存在

python - 在 Python2.x 中使用 print()(函数版本)

python - 如何将函数传递给 Python 中的函数?

linux - 在 ubuntu bash 脚本中,当我尝试转换错误的十六进制值时不会抛出任何错误

javascript - NodeJS 中不带换行符的日志记录

bash - 如何从最后一个字段作为变量的bash调用awk

javascript - 使用 javascript 将平面数组转换为嵌套数组

java - 使用 jackson 动态嵌套 json 字符串到 java 对象

c++ - 使用二维数组作为参数的函数调用