mana
mana

Reputation: 13

why does $ssh->exec(..) doesn't execute when we have dynamic variables as part of command

I am unable to run the below command when i pass the values as $start & $end but it runs successfully when i give the date values directly. Is there any other way of running the command? Note : I am using phpseclib and able to successfully login and execute basic commands.

$start value is "2015-07-27 18:48:56" $end value is "2015-07-27 18:49:00"

echo $ssh->exec('/tpo/umc/bin/cmu_monitoring_dump -n r02bv01 -m cpunumber -t0 "$start" -t1 "$end" -f line> /tmp/adwant.txt'); 

Upvotes: 0

Views: 317

Answers (1)

Sherif
Sherif

Reputation: 11943

The short answer is that you cannot nest double and single quoted strings, which appears to be what you're trying to do here and expecting the result to be that PHP will expand your variables into the string. Once the string syntax begins with single quotes, no characters inside of the string will be expanded (re-evaluated), which is unlike double quotes. So double quote inside of a single quoted string is still just a literal double quote.

Instead use echo $ssh->exec("/tpo/umc/bin/cmu_monitoring_dump -n r02bv01 -m cpunumber -t0 $start -t1 $end -f line> /tmp/adwant.txt");

The Long Answer

In PHP there are 4 main syntax for strings (since PHP 5.3, anyway).

  1. Double Quoted Strings
  2. Single Quoted Strings
  3. Heredoc
  4. Nowdoc

Heredoc and Nowdoc syntax will produce the same net result as Double and Single quoted strings, respectively.

The only difference between single and double quote strings is that single quote strings do no offer string interpolation and only have one escape sequence character (the single quote itself). That is to say, that when you create a string like 'some $string here' it will become a string literal of every character between the opening and closing single quote with no other characters that can be expanded within the string. Same thing applies to Nowdoc string syntax.

Caveats

Now, assuming you want literal double quotes in the string you need to escape those with a backslash \ escape character. See escaping double quote strings in the manual.

You should also note that there is a difference in some shells between double and single quotes as well, outside of PHP's string parsing, and that those shells may have different escape sequence characters as well (see escapeshellarg for more details).

Upvotes: 3

Related Questions