无法在 shell_exec 函数中传递参数

Not able to pass arguments in shell_exec function

我对使用 shell 还很陌生 commands.I 我在 php.I 中做一个调试器应用程序需要检查 .exe 文件的结果以检查代码是 right.So 我尝试了一个测试程序。

C 程序是

包括

#define MAX 128
int main(char c)
{
const int max=127;
char array[max]; // char array[10];
char string[MAX];
scanf("%c",&c);
array[0] = string[0] = c;
printf("%c %c\n", array[0], string[0]);
return 0;
}

编译为se.exe

PHP 代码

<?php 
define('STDIN',fopen("php://stdin","r"));
$cor1=1;
$op=shell_exec("se.exe H");
echo($op);  
if($op=="H H")
$cor1+=1;
echo $cor1;
if($cor1>1)
{echo "PASSED";}
else
{echo "FAILED";}
?>

这没有回显任何值。

c 程序正在下面的行中打印一个新行 \n

printf("%c %c\n", array[0], string[0]);

在php代码中,比较字符串不包括下一行中的换行符

if($op=="H H")

我建议您更改 php 代码以比较包括换行符(使用 strcmp 比较字符串。不要使用 == 运算符比较字符串)

if(strcmp($op, "H H\n") == 0)

希望对您有所帮助。

我认为该论点已成功通过。不使用参数的 c 代码。它正在 scanf 获取用户输入,而不是真正使用正在传递的参数。您需要更改 c 代码以使用参数。

int main(int argc, char *argv[]) { 
    /* ... 
    argv[0] will have the executable name as a char array (char *)
    argv[1] will have the first command line argument as a char array (char *)
    argv[1][0] will have the first character of the first parameter that you seem to be interested.
    ensure that you do proper null check or you will get core dump/application crash
    */ 
}