gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:24:15 24 4
gpt4 key购买 nike

我需要知道 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com