سؤال

If you create a simple php script with this code:

shell_exec('"');

And run it with:

php myscript.php

It gives the following error in bash on my Mac:

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

I've tried everything I can think of:

ob_start();
@shell_exec('"');
ob_end_clean();

@shell_exec('" 2> /dev/null');

But no matter what I try, I can't suppress the message. The problem is that I'm creating a unit test to stress test $argc, $argv and the getopt() command for a general purpose tool, and I need to call the script hundreds of times with various inputs containing random characters like '"=: etc.

So I need to be able to either suppress the error output, or detect imbalanced quotes and append either a ' or " to the end of the string. I found this regex to detect single and double quoted strings:

PHP: Regex to ignore escaped quotes within quotes

But I'm having trouble visualizing how to do this for a general bash command that has a mix of quoted and unquoted arguments. Is there a built-in command that would tell me if the string is acceptable without throwing an error? Then I could try appending either "'" or '"' and I'm fairly certain that one of them would close the string. I'm only concerned with preventing the error message for now, because I'm just throwing random input at the script at this point.

Thanks!

هل كانت مفيدة؟

المحلول

The child shell process is writing the error message to STDERR, which it inherited from the parent PHP process. You could close the parent's STDERR file handle in the PHP script before running shell_exec().

<?php
fclose (STDERR);
shell_exec('"');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top