1

I want to create a stop.sh file for stopping screen session.

$test = "screen_name";

This is the command:

kill -15 $(screen -ls | grep '[0-9]*\.$test' | sed -E 's/\s+([0-9]+)\..*/\1/'`)

And I want to create this file with php ssh2_exec like this:

ssh2_exec($connection, "echo 'kill -15 $(screen -ls | grep '[0-9]*\.$test' | sed -E 's/\s+([0-9]+)\..*/\1/')' > /home/test/stop.sh");

But I get this:

sh: 1: Syntax error: "(" unexpected

I tried:

kill -15 $(screen -ls | grep \'[0-9]*\.$test\' | sed -E \'s/\s+([0-9]+)\..*/\1/\')

But this is not working.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • You're missing `(` – executable Jan 28 '22 at 10:54
  • Within double quotes PHP will try to parse code that begins with `$` so in that command where you have `$(screen -ls.....` my belief is that PHP baulks at that bracket because no variable would begin with a bracket. You should try escaping that `$` – Professor Abronsius Jan 28 '22 at 11:22

2 Answers2

0

As per the comment regarding parsing anything within double quotes that begins with a $ you could try an alternative to construct the command string by using sprintf and wrapping the whole command in single quotes but with double quotes within.

$test='banana';

$cmd=sprintf('kill -15 $(screen -ls | grep "[0-9]*\.%s" | sed -E "s/\s+([0-9]+)\..*/\1/") > /home/test/stop.sh', $test );
echo $cmd;

Which yields a finalised command string:

kill -15 $(screen -ls | grep "[0-9]*\.banana" | sed -E "s/\s+([0-9]+)\..*/\1/") > /home/test/stop.sh

which looks OK, so you could then do:

ssh2_exec( $connection, $cmd );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

What about trying this:

$text = 'echo \'kill -15 $(screen -ls | grep \'[0-9]*\.'$test.'\' | sed -E \'s/\s+([0-9]+)\..*/\1/\')\' > /home/test/stop.sh\' ;
ssh2_exec($connection, $text);
SPL
  • 23
  • 6