Page 1 of 1

escapeshellarg problem

Posted: Mon Dec 01, 2008 5:38 am
by mark007
Hi all,

I am trying to pass information from a php script as an argument to a shell script which in turn writes the text to a file, so a simple transfer of text.

I am using the escapeshellarg function to make sure I have single quotes around my string and to make sure nothing goes wrong BUT it seems like escapeshellarg is doing more than I want it to do.

If I try to pass a simple string like this accross

Code: Select all

 
$value="abcd \\\\ defg ////";
$arg=escapeshellarg($value);
 
$Shell_Command=$HOME."/scripts/write_file.sh " . $arg;
shell_exec($Shell_Command);
 
My shell script shows the string to be missing two of the backslashes like this
"abcd \\ defg ////"

I thought this might be a problem with my shell script, but if I simply do an echo in php, I see that its php thats messing with my string
echo $arg; -->> shows "abcd \\ defg ////" on screen also.

Where are the two backslashes going and how can I stop php doing anything other than making the string safe as regards quotes (that is, don't get rid of any characters on me).

Regards,

Re: escapeshellarg problem

Posted: Mon Dec 01, 2008 5:57 am
by requinix
You could do something complicated, or you could just remember that if you want a backslash in a PHP string you have to double it.

Code: Select all

$value="abcd \\\\\\\\ defg ////";

Re: escapeshellarg problem

Posted: Mon Dec 01, 2008 6:07 am
by mark007
oh lordie, thats it? , php strings need backslashes doubled, is there a list of these or is backslash the only one?

I really need this to be perfect, aka, pass whatever the string is, exactly as is to the shell script...

Thanks alot!

Re: escapeshellarg problem

Posted: Mon Dec 01, 2008 6:22 am
by requinix