Bash函数数组

Ato*_*lan 5 arrays bash function

我正在尝试创建一个函数数组,以便按顺序遍历每个函数.

declare -a FUNCTION
FUNCTION[1]="FUNCTION.A"
FUNCTION[2]="FUNCTION.B"
FUNCTION[3]="FUNCTION.C"

for i in "${!FUNCTION[@]}"; do
  ${FUNCTION[$i]};
done
Run Code Online (Sandbox Code Playgroud)

这只是打印出FUNCTION.A并说找不到命令.我需要它来运行该功能.建议?

PSk*_*cik 9

对我来说很好.

declare -a FUNCTION
FUNCTION[1]="FUNCTION.A"
FUNCTION[2]="FUNCTION.B"
FUNCTION[3]="FUNCTION.C"

#Define one of the functions
FUNCTION.A() { echo "Inside of FUNCTION.A"; }


$ for i in "${!FUNCTION[@]}"; do   ${FUNCTION[$i]}; done
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Inside of FUNCTION.A
FUNCTION.B: command not found
FUNCTION.C: command not found
Run Code Online (Sandbox Code Playgroud)


Ato*_*lan 1

我是个白痴...... 该函数显然必须位于其被调用的位置之上。有点烦人。我希望所有的功能都可以驻留在底部。