Say there is a programme that requires input in form <prog> --param "one;two;three"
: a flag --param
followed by a string (enclosed in quotes) of words separated by a semicolon. For example, the following Bash scrip can be used:
flag=${1}
items=(${2//;/ })
echo "flag: ${flag}"
echo "items: ${items[@]}"
Clearly, the output would be:
flag: --param
items: one two three
The delimiter ;
is a must and therefore we need to pass a string enclosed in quotes, otherwise shell will treat things between semicolons as separate commands.
This question is somewhat similar to this one, but the answer proposed there doesn't work for the following.
Consider this CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.16.2)
project(clumsy)
add_custom_command(OUTPUT foo.txt
COMMAND bash ${CMAKE_SOURCE_DIR}/script.sh --param "one;two;three" &>> foo.txt
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
VERBATIM
)
add_custom_target(foo DEPENDS foo.txt)
set(PARAMS "one;two;three")
add_custom_command(OUTPUT bar.txt
COMMAND bash ${CMAKE_SOURCE_DIR}/script.sh --param "${PARAMS}" &>> bar.txt
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
VERBATIM
)
add_custom_target(bar DEPENDS bar.txt)
Running make foo
after configuring works as expected:
[100%] Generating foo.txt
flag: --param
items: one two three <----
[100%] Built target foo
However make bar
results in wrong output because the variable PARAMS
was expanded as list and the actual input was --param one two three
instead of --param "one;two;three"
:
[100%] Generating bar.txt
flag: --param
items: one <----
[100%] Built target bar
How to use variable PARAMS
and still make the input for the external command to consist of two arguments?
UPDATE
I have made the code of CMakeLists.txt
more complicated with use of output redirects involving &
(like &>>
) which conflicts with use of VERBATIM
flag which is important to make variable quoting work.