In a build script I need to execute a command on a file that is created by the user. I cannot control the path of this file, and if the user creates a path that includes a blank, my build script crashes.
You can reproduce the problem in this way: 1) create a directory from the shell prompt, with a name that includes a blank (please note the blank between the word “test” and the word “dir”; I need to surrount this path with quotes to make the shell happy)
mkdir "/tmp/test dir"
- now try to execute a ls of that dir with the following task:
task testCommandTask << {
String l_aPathThatIncludesABlank="/tmp/test dir"
String l_command="ls -l ${l_aPathThatIncludesABlank}" // broken, error because path includes blanks.
// ls complains: cannot access /tmp/test: No such file or directory
Process l_process = l_command.execute()
l_process.waitFor()
String l_stdout = l_process.in.text
String l_stderr = l_process.err.text
assert (l_process.exitValue() == 0), "DETAILS: failure executing external command."
}
- I’ve also tried surrounding the path with double quotes, single quotes but found no way to solve this issue
String l_command=“ls -l “${l_aPathThatIncludesABlank}”” // broken, looks for file “/tmp/test and then looks from file dir” String l_command=“ls -l ‘${l_aPathThatIncludesABlank}’” // broken, looks for file ‘/tmp/test and then looks from file dir’
- I’ve also tried escaping the blank with a slash, but even this attempt failed
String l_aPathThatIncludesABlank="/tmp/test\ dir" String l_command=“ls -l ${l_aPathThatIncludesABlank}” // broken, looks for file ‘/tmp/test and then fro file dir’
Can you help me? Thanks, Luca
N.B.: It seems that the parser has some problem showing you the task code, so I didn’t use a code marker around it.