1

I'm having trouble adding -j 4 to my make command. Its causing my Bash script to fail with:

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

Here's the Bash that determines when to add -j 4. It seems to mostly be working:

# $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

Then, the Bash that invokes it and cause the error:

"$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

There are similar questions on Stack Overflow with other commands, like 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 don't seem to work in this instance.

How do I append -j 4 to $MAKE?


I also tried the following,:

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

But it resulted in:

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

Finally, there's 50 to 75 of these:

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

So I want to fix 1 "$MAKE", and not 50 or 75 uses of it.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • Are you doing this in windows? On MS-DOS, the ‘-j’ option has no effect, since that system doesn’t support multi-processing. If the ‘-j’ option is followed by an integer, this is the number of recipes to execute at once; this is called the number of job slots. If there is nothing looking like an integer after the ‘-j’ option, there is no limit on the number of job slots. The default number of job slots is one, which means serial execution (one thing at a time). – NinjaGaiden Jan 19 '16 at 03:18
  • @user3589054 - It is Linux. In this case, its a Ubuntu 14.04.3 LTS Server. The script is at [cryptest.sh](http://github.com/weidai11/cryptopp/blob/master/cryptest.sh). – jww Jan 19 '16 at 03:20
  • Why do you use double quotes in `"$MAKE" static dynamic ...` ? – mauro Jan 19 '16 at 03:32
  • http://mywiki.wooledge.org/BashFAQ/050 – tripleee Jan 19 '16 at 05:35

1 Answers1

3

You are trying to run the command named "make -j 4", not the command "make" with arguments "-j" and "4". In this case, you would run your command with simply

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

that is, without quoting the expansion of $MAKE. However, in general you should not store command invocations in a variable, only command names. Store the arguments in (preferably) an array.

MAKE=make
MAKEARGS=( -j 4 )

"$MAKE" "${MAKEARGS[@]}" static dynamic cryptest.exe ...
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks Chepner. So I am clear... I invoke make *without* the quotes using ***`$MAKE`***, and I form the command with ***`MAKE="$MAKE -j 4"`***. Is that correct? – jww Jan 19 '16 at 03:52
  • @jww: Yup, that's the trivial fix; but it's not a very robust or extensible approach, which is why this answer is longer than that. – tripleee Jan 19 '16 at 05:34
  • See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Jan 19 '16 at 06:10