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

问题描述 投票:0回答:1

我需要知道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 nested function-call
1个回答
1
投票

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

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

这利用了DEBUG陷阱和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="$n"
    if [ "$poplevel" -gt 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

结果:

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(9)(8)(7)(6)(5)(4)(3)(2)(1)
2: 1 2 3 4 5 6 7 8 9(8)(7)(6)(5)(4)(3)(2)(1)
3: 1 2 3 4 5 6 7 8 9(7)(6)(5)(4)(3)(2)(1)
4: 1 2 3 4 5 6 7 8 9(6)(5)(4)(3)(2)(1)
5: 1 2 3 4 5 6 7 8 9(5)(4)(3)(2)(1)
6: 1 2 3 4 5 6 7 8 9(4)(3)(2)(1)
7: 1 2 3 4 5 6 7 8 9(3)(2)(1)
8: 1 2 3 4 5 6 7 8 9(2)(1)
9: 1 2 3 4 5 6 7 8 9(1)
10: 1 2 3 4 5 6 7 8 9
© www.soinside.com 2019 - 2024. All rights reserved.