0

I am trying to run 'make' command from my bash script to build the code.

I can see that all parameters got assigned and able to display the command that i am trying to run. I could not see any issue with the command. But the issue is when it tries to run the command via bash script it fails.

My command is :- ./build_script LIC=1 DOCUMNETS=1 PROJECTS="cuda bfm"

Script Snippet of parsing all the arguments and constructing make command:-

makeargs=""
for build_flag do
   if [[ "$build_flag " = "PROJECTS="* ]]; then
          apps =`echo $build_flag |sed "s/PROJECTS=//" `
          makeargs="$makeargs PROJECTS=\"$apps \""
   else
        makeargs="$makeargs $build_flag"
    fi
done
echo "make DCOV=1 $makeargs $maketest"
make DCOV=1 $makeargs $maketest

When i run the script, I can see the build command has constructed properly.

Output :-

make DCOV=1 LIC=1 DOCUMNETS=1 PROJECTS="cuda bfm" run_all
GNUmakefile:16: warning: overriding commands for target `/'
GNUmakefile:19: warning: ignoring old commands for target `/'
make: *** No rule to make target `bfm"'.  Stop.

I try to print PROJECTS variable in my 'GNUmakefile' present in build_main folder. I can see the output : PROJECTS is "bfm . It is not taking whole "cuda bfm" as a whole string.

Note:- When i try to run the same build command :- make DCOV=1 LIC=1 DOCUMNETS=1 PROJECTS="cuda bfm" run_all explicitly it works fine.

Seems like issue with Interpreting variables with makefile.

Any solution for this ? Please help. Thanks!

5
  • Can't see enough of your script. Either add more code, or paste your code into shellcheck.net or run your script with bash -x build_script ... Commented Feb 20, 2020 at 8:20
  • Hi Mark, As you can see the command is getting generated properly right? the for loop will parse the arguments and check for PROJECTS string and generates a build command. Commented Feb 20, 2020 at 9:16
  • I can't see enough of your code to tell what's wrong. Did you post it in spellcheck.net yet? Because, for example this is incorrect make DCOV=1 $makeargs $maketest as you haven't double quoted "$makeargs" or "$maketest" Commented Feb 20, 2020 at 9:37
  • No, I haven't posted in speelcheck.net before. We are running it as a build command i.e. make .... So its not necessary to keep double quotes right ? Commented Feb 20, 2020 at 9:43
  • Exactly, that's why I made the initial suggestions of running under bash -x and using shellcheck... Commented Feb 20, 2020 at 9:51

1 Answer 1

2

Change makeargs string to array before passing it as an arguments group.

eval makeargs_array=( $makeargs )
make UVC=1 "${makeargs_array[@]}" $maketest

Without converting to array, if you enable debug, it shows last line interpretation as

make DCOV=1 LIC=1 DOCUMNETS=1 'PROJECTS="cuda' bfm '"'

Which is clearly ignoring double-quote and considering space as separator. Even double-quote is getting passed as a separate argument in this case.

Explanation:

Word-splitting It says,

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

If we use "$makeargs" i.e. surrounded by double-quote, it is not considered by word-splitting and results in LIC=1 DOCUMNETS=1 "PROJECTS=cuda bfm"

But again its a complete string, while requirement is to split the string to use as arguments.

So now using $makeargs.

Word-splitting gets in action as per the default IFS (space, tab, newline), we get result as LIC=1 DOCUMNETS=1 PROJECTS="cuda bfm "

Double-quoted part of string didn't affect the word-splitting since, subject to splitting is complete string here.

Why array worked here?

Array itself expands each element as separate word when using @ and here no further word-splitting requires after expansion.

Arrays

Sign up to request clarification or add additional context in comments.

1 Comment

May i know why bash is interpreting this way ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.