"make -j 4: command not found" 从脚本调用 make 时

"make -j 4: command not found" when invoking make from a script

我无法将 -j 4 添加到我的 make 命令。它导致我的 Bash 脚本失败:

./cryptest.sh: line 208: make -j 4: command not found
ERROR: failed to make cryptest.exe

这里的Bash决定了何时添加-j 4。它似乎大部分都在工作:

# $MAKE is already set and either 'make' or 'gmake'
CPU=$(cat /proc/cpuinfo | grep -c '^processor')
if [ "$CPU" -gt "1" ]; then
    echo "$CPU processors, using \"$MAKE -j $CPU\""
    MAKE="$MAKE -j $CPU"
fi

然后,调用它并导致错误的Bash:

"$MAKE" static dynamic cryptest.exe 2>&1 | tee -a "$TEST_RESULTS"
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
        echo "ERROR: failed to make cryptest.exe" | tee -a "$TEST_RESULTS"
fi

Stack Overflow 上也有与其他命令类似的问题,例如 Execute command as a string in Bash, but its not obvious to me how to simply append the command's arguments to the command. And doing the obvious results in errors like above, so questions like How can I concatenate string variables in Bash 在这种情况下似乎不起作用。

如何将 -j 4 附加到 $MAKE ?


我也尝试了以下方法:

MAKE="$MAKE" "-j $CPU"

但结果是:

./cryptest.sh: line 186: -j 4: command not found

最后,有 50 到 75 个:

export CXXFLAGS="..."
"$MAKE" static dynamic ...

所以我想修复 1 "$MAKE",而不是 50 或 75 次使用。

您正在尝试 运行 名为 "make -j 4" 的命令,而不是带有参数“-j”和“4”的命令 "make"。在这种情况下,您可以 运行 您的命令只需

$MAKE static dynamic cryptest.exe 2>&1 ...

即不引用 $MAKE 的扩展。但是,通常您不应将命令调用存储在变量中,而应仅存储命令 names。将参数存储在(最好)数组中。

MAKE=make
MAKEARGS=( -j 4 )

"$MAKE" "${MAKEARGS[@]}" static dynamic cryptest.exe ...