Description
Symfony version(s) affected: 4.4.x
Description
This affects when the commands passed to Process are strings, instead of arrays (which some commands need in order to process correctly).
The assumption here is the use of the Process::fromShellCommandline()
static method.
When a command string includes variables that the command itself sets, Process returns exception Command line is missing a value for key %s: %s.
from method replacePlaceholders
.
These variables may not be able to be predefined as environment variables, leading us to a situation where we cannot move forward.
How to reproduce
// Multi-line command which includes a shell/bash variable that is not parsed by PHP:
$cmd = 'SOMECOUNT=0
if [[ "$SOMECOUNT" == 0 ]]; echo "it is zero"; fi'
$p = Process::fromShellCommandline($cmd);
$p->run();
Result:
Symfony/Component/Process/Exception/InvalidArgumentException with message 'Command line is missing a value for key "$SOMECOUNT": SOMECOUNT=0
if [[ "$SOMECOUNT" == 0 ]]; echo "it is zero"; fi.'
Possible Solution
I've so far only been able to get around this via this "trick", which replaces the variable $SOMECOUNT
with an equivalent string content:
$cmd = 'SOMECOUNT=0
if [[ "$SOMECOUNT" == 0 ]]; echo "it is zero"; fi'
$p = Process::fromShellCommandline($cmd);
$p->run(null, ['SOMECOUNT' => '$SOMECOUNT']);
Additional context
This doesn't happen in all cases. For example, this works:
$cmd = 'SOMETHINGGENERATED=`date +"%m-%d-%y"`; echo $SOMETHINGGENERATED';
$p = Process::fromShellCommandline($cmd);
$p->run(); // No exception
I don't know what the difference is between the 2. Perhaps something in the regex.