我有一个第三方生成器,它是我构建过程的一部分(sbt原生打包程序)。它生成一个bash脚本,用于运行我构建的程序。
问题是我需要使用sh (ash),而不是bash。所以生成器会像这样写出一行:
declare -a app_mainclass=("com.mypackage.Go")sh在这上面卡住了,因为没有'declare‘命令。
聪明的我--我只是添加了以下几行:
alias declare=''
alias '-a'=''这在除了这个声明之外的所有这样的声明上都有效--因为有了括号。sh显然没有数组。
假设我实际上不能更改生成器,那么我可以做些什么来欺骗sh代码使其正常运行呢?在这种情况下,从逻辑上讲,我想去掉括号。(如果我在生成的输出中手动执行此操作,效果会很好。)
我正在考虑尝试定义一个函数app_mainclass= () { app_mainclass=$1; },但是sh不喜欢这样--抱怨(.不确定是否有办法将'=‘作为函数名的一部分。
有没有办法让sh接受这个生成的命令(括号)?
发布于 2015-07-01 05:27:51
我不愿提出这个建议,但是您可以尝试使用一个使用eval的函数声明来执行declare语句产生的任何赋值。在使用它之前,我可能会验证生成的declare语句是“安全的”。(例如,赋值不包含任何可能被eval作为任意代码执行的内容。)
declare () {
array_decl=
for arg; do
# Check if -a is used to declare an array
[ "$arg" = -a ] && array_decl=1
# Ignore non-assignment arguments
expr "$arg" : '.*=.*' || continue
# Split the assignment into separate name and value
IFS='=' read -r name value <<EOF
$arg
EOF
# If it's an array assignment, strip the leading and trailing parentheses
if [ -n "array_decl" ]; then
value=${value#(}
value=${value%)}
fi
# Cross your fingers... I'm assuming `$value` was already quoted, as in your example.
eval "$name=$value"
done
}https://stackoverflow.com/questions/31065071
复制相似问题