linux - 遍历函数名数组并在 bash 脚本中执行所有这些函数名
问题描述
我编写了一个 bash 脚本,我有一个函数名数组,其中传递了变量,我想在一个循环中执行所有这些。
但是当我执行 bash 脚本时,我得到了这个错误:
a: 找不到命令
我怎样才能做到这一点?
我的 bash 脚本如下所示:
#!/bin/bash
functions_array=("test a" "test b" "testc")
test() {
echo $1
}
testc() { echo "testc!"; }
for i in ${functions_array[@]}; do
${i}
done
解决方案
你得到这个错误是因为你没有引用你的变量。因此test a
分为两部分。
试试这样:
#!/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
推荐阅读
- dataset - 用 Scapy 合成数据包
- java - 无法在 feign builder 中引发期望
- php - 在 PHP 中从 SFTP 获取最新文件(使用 CURL)
- salt-stack - 状态应用期间的 Saltstack 发送事件
- c - 将地址或指针传递给 C 函数
- facebook - Facebook - 如何从 Instant Games 中删除加载屏幕
- php - 如何在我的服务中使用学说方法(Symfony 4)?
- javascript - 在服务器调用中获取部分 redux 状态
- node.js - Node.js - 使用 MSSQL 包动态添加多个参数
- javascript - 打印多个 iframe 内容