bash to sh (ash) 欺骗 [英] bash to sh (ash) spoofing

查看:65
本文介绍了bash to sh (ash) 欺骗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第 3 方生成器,它是我构建过程的一部分(sbt 本地打包器).它生成一个用于运行我构建的程序的 bash 脚本.

I have a 3rd party generator that's part of my build process (sbt native packager). It generates a bash script to be used to run my built program.

问题是我需要使用 sh (ash),而不是 bash.所以生成器输出如下一行:

Problem is I need to use sh (ash), not bash. So the generator cranks out a line like this:

declare -a app_mainclass=("com.mypackage.Go")

sh 对此感到窒息,因为没有 'declare' 命令.

sh chokes on this as there is no 'declare' command.

聪明的我--我刚刚添加了以下几行:

Clever me--I just added these lines:

alias declare=''
alias '-a'=''

这适用于除此之外的所有此类声明 - 因为有括号.sh 显然没有数组.

This worked on all such declarations except this one--because of the parens. sh apparently has no arrays.

鉴于我实际上无法更改生成器,我该怎么做才能欺骗 sh 代码以使其正常运行?在这种情况下,我从逻辑上想消除括号.(如果我在生成的输出中手动执行此操作,则效果很好.)

Given that I cannot practically change the generator, what can I do to spoof the sh code to behaving properly? In this case I logically want to eliminate the parens. (If I do this manually in the generated output it works great.)

我想尝试定义一个函数 app_mainclass= () { app_mainclass=$1;} 但 sh 不喜欢那样 - 抱怨 (.不确定是否有办法将 '=' 作为函数名称的一部分.

I was thinking of trying to define a function app_mainclass= () { app_mainclass=$1; } but sh didn't like that--complained about the (. Not sure if there's a way to include the '=' as part of the function name or not.

有什么办法可以诱使 sh 接受这个生成的命令(括号)?

Any ideas of a way to trick sh into accepting this generated command (the parens)?

推荐答案

我对建议它犹豫不决,但您可以尝试使用 eval 来执行由 产生的任何赋值的函数声明声明 语句.在使用它之前,我可能会验证生成的 declare 语句是安全的".(例如,分配的值不包含任何可能被 eval 作为任意代码执行的内容.)

I hesitate to suggest it, but you might try a function declaration that uses eval to execute any assignments produced by a declare statement. I might verify that the generated declare statements are "safe" before using this. (For example, that the assigned value doesn't contain any thing that might be executed as arbitrary code by 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
}

这篇关于bash to sh (ash) 欺骗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆