Question

I'm trying to wirte a bash script that will dynamically construct the command I need to execute. (merging lcov files)

So far I'm able to construct the command line, here is the result:

#!/bin/bash
LCOV_INPUT_FILES=""
while read FILENAME; do
    LCOV_INPUT_FILES="$LCOV_INPUT_FILES -a \"$FILENAME\""
done < <( find ../reports/coverage -name lcov.info )

echo lcov "${LCOV_INPUT_FILES}" -o ../reports/coverage.lcov
lcov "${LCOV_INPUT_FILES}" -o ../reports/coverage.lcov

If I run the line the echo outputs in my shell it works, but I cannot run this command from my script, and I lack of bash greatness to debug it.

Here is the kind of output I get from the echo:

lcov  -a "../reports/coverage/Chrome 26.0 (Linux)/lcov.info" -a "../reports/coverage/Firefox 20.0 (Linux)/lcov.info" -a "../reports/coverage/PhantomJS 1.8 (Linux)/lcov.info" -o ../reports/coverage.lcov

Do you know what could be wrong here?

Regards,

Xavier

Was it helpful?

Solution

If you use double quotes "${LCOV_INPUT_FILES}", bash treats the whole string as one word. Without double quotest, bash would split the variable's value on whitespace.

For the way you constructed the string the only option is to use eval:

eval lcov "$LCOV_INPUT_FILES" -o ...

There are other options, too. You can use an array to store the parameters

LCOV_INPUT_FILES+=(-a "$FILENAME")

and then just expand the array in double quotes:

lcov "${LCOV_INPUT_FILES[@]}" -o ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top