Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Process] Process::fromShellCommandline() raises exception when command contains bash/shell variables #34838

Closed
@fideloper

Description

@fideloper

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions