我想要一个脚本,以便在每次安装我的ubuntu时都能轻松安装软件包/程序/工具,我希望它是模块化的,可以轻松添加/删除软件包,但是使用eval调用函数时似乎存在问题。
我的代码的简化版本是:
#!/bin/bash
ARRAY=("program 1" true fn_program1, "program 2" true fn_program2)
function fn_program1 {
echo "IT'S WORKING!!"
}
function fn_program2 {
echo "IT'S WORKING TOO!!"
}
for ((i=0; i<${#ARRAY[@]}; i+=3)); do
if [ ! "${ARRAY[$i+1]}" = "false" ]; then
eval ${ARRAY[$i+2]}
fi
done
这是执行
时的控制台Linux$ ./script.sh
./script.sh: línea 18: fn_program1,: command not found
IT'S WORKING TOO!!
注意我们如何添加新功能
#!/bin/bash
ARRAY=("program 1" true fn_program1, "program 2" true fn_program2, "program 3" true fn_program3)
function fn_program1 {
echo "IT'S WORKING!!"
}
function fn_program2 {
echo "IT'S WORKING TOO!!"
}
function fn_program3 {
echo "IT'S WORKING THREE!!"
}
for ((i=0; i<${#ARRAY[@]}; i+=3)); do
if [ ! "${ARRAY[$i+1]}" = "false" ]; then
eval ${ARRAY[$i+2]}
fi
done
这是执行
时的结果~/Linux$ ./script.sh
./script.sh: línea 21: fn_program1,: command not found
./script.sh: línea 21: fn_program2,: command not found
IT'S WORKING THREE!!
我似乎无法弄清楚为什么会发生这种情况以及如何解决这个问题。有什么想法吗?