[英]Loop through array of function names and execute all of them in a bash script
I write a bash script and i have an array of function names with variable passed to them and i want to execute all of them in a loop.我编写了一个 bash 脚本,我有一个 function 名称数组,其中传递了变量,我想在循环中执行所有这些名称。
but when i execute the bash script, i get this error:但是当我执行 bash 脚本时,我收到此错误:
a: command not found a: 找不到命令
how can i do this?我怎样才能做到这一点?
my bash script is look like this:我的 bash 脚本如下所示:
#!/bin/bash
functions_array=("test a" "test b" "testc")
test() {
echo $1
}
testc() { echo "testc!"; }
for i in ${functions_array[@]}; do
${i}
done
You get this error because you didn't quote your variables.你得到这个错误是因为你没有引用你的变量。 Therefore test a
is split into two parts.因此test a
分为两部分。
Try it like this:试试这样:
#!/bin/bash
functions_array=("test a" "test b" "testc")
test() {
echo "$1" # quoting here and ...
}
testc() { echo "testc!"; }
for i in "${functions_array[@]}"; do # also here
${i}
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.