代码之家  ›  专栏  ›  技术社区  ›  Basile Starynkevitch

如何将一个元素附加到GNUBASH数组变量,并将该数组变量用作ELF可执行文件的参数

  •  0
  • Basile Starynkevitch  · 技术社区  · 5 年前

    在 Bismon 静态源代码分析器(GPLv3+许可), git commit 49dd1bd232854a GCC 10 直接编译 Debian 我有一个测试 Bash script Hello-World-Analyze bismon_hello_args .

    该变量的声明(在第56行)使用:

    declare -a bismon_hello_args
    

    我想填补这个空缺 --bismon ,然后调用 bismon main 是其中的要素 比斯蒙你好 变量

    所以如果我的 你好,世界分析 Hello-World-Analyze --bismon-debug-after-load --bismon-anon-web-cookie=/tmp/bismoncookie --gcc=/usr/local/bin/gcc-11 我想要这个 比斯蒙 用两个参数启动ELF可执行文件(用C的说法,是argc=3): --bismon-debug-after-load 然后 --bismon-anon-web-cookie=/tmp/bismoncookie

    出于某种原因,以下代码(第58到64行)在 脚本:

    for f in "$@"; do
        case "$f" in
        --bismon*) bismon_hello_args+=$f;;
        --asm) export BISMON_PLUGIN_ASMOUT=/tmp/gcc10_metaplugin_BMGCC.s;;
        --gcc=*) export BISMON_GCC=$(echo $f | /bin/sed -e s/--gcc=//);;
        esac
    done
    

    没有按预期工作。它应该是(而且是在以前的版本中) git commit e8c3d795bc9dc8

    ./bismon $bismon_hello_args &
    

    但调试结果表明 比斯蒙 使用 argc=2 argv[1] 程序参数。。。

    我做错了什么?

    1 回复  |  直到 5 年前
        1
  •  2
  •   tripleee    5 年前

    仅仅 += 将字符串添加到现有字符串中。你可能想要 bismon_hello_args+=("$f");; (另请注意引号)。要调用该程序,请使用 ./bismon "${bismon_hello_args[@]}" & (请再次注意引号)。

    使用数组变量的语法与使用简单标量的语法不同。此语法继承自 ksh ,这反过来需要找到一种方法来引入新的行为,而不牺牲与现有Bourne shell脚本的兼容性。

    如果没有数组修饰符,Bash只需 accesses the first element of the array. (这让初学者和有经验的从业者都感到困惑。)

    推荐文章