Domanda

I'm trying to use the following command in php to send to my server:

ssh2_exec($con,  'sed -i \'s/max-players=4/max-players=8/g\' ' . $filedir . '\'/server.properties')

But it just doesn't do anything. This command on the other hand:

ssh2_exec($con, 'sed -i \'s/max-players=4/max-players=12/g\' /home/servers2/\'' . $mUsername . '\'/server.properties')

Works just fine, I'm trying to change from having the hardcoded dir to a dir from a db. So

$filedir = $DIR . $mUsername;

$Dir in this case is '/home/servers2/' and $mUsername can be Testing540

When I printed the value of $filedir I got:

/home/servers2/Testing540

Are my quotes escaped right in the first code snippet?

È stato utile?

Soluzione

Your question about correct quoting is hard to answer, because you don't create something that you can easily debug. Consider the following code instead:

$pattern = 'sed -i \'s/max-players=4/max-players=8/g\' %s\'/server.properties';
$command = sprintf($pattern, $filedir);
$result  = ssh2_exec($con,  $command);

Then you can more easily validate your actual command, e.g. by inspecting / outputting / logging: $command.

Also you can more easily compare what the difference between the two variants is.

Also enable error reporting and logging to the highest level, you might just have the problem that the variable $DIR is not defined:

Notice: Undefined variable: DIR in ...

This will make your first command to fail because there is no such path/file then. That is highly likely your issue.

Take care.

Altri suggerimenti

Turns out the problem was at the end of the line. I change it to:

ssh2_exec($con,  'sed -i \'s/max-players=4/max-players=8/g\' ' . $filedir . '/server.properties')

And it worked great. I had to take out the \' right before /server.properties'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top