dsgsdg206050
2017-02-02 04:48
浏览 156
已采纳

无法在shell_exec函数中传递参数

I am pretty new to using the shell commands.I am doing a debugger application in php.I need to check the result of the .exe files to check whether the logic of the code is right.So I tried a test program .

The C program is

include

#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
", array[0], string[0]);
return 0;
}

This is compiled as se.exe

PHP CODE

<?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";}
?>

This is not echoing any values.

图片转代码服务由CSDN问答提供 功能建议

我很擅长使用shell命令。我在php中做一个调试器应用程序。我需要检查一下 .exe文件的结果,以检查代码的逻辑是否正确。所以我尝试了一个测试程序。

C程序是

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

这是编译为se.exe \ n

PHP CODE

 &lt;?php 
define('STDIN',fopen(“php:// stdin”,“r”)); \  n $ cor1 = 1; 
 $ op = shell_exec(“se.exe H”); 
echo($ op);  
if($ op ==“HH”)
 $ cor1 + = 1; 
echo $ cor1; 
if($ cor1&gt; 1)
 {echo“PASSED”;} 
else 
 {echo“FAILED”;  } 
?&gt; 
   
 
 

这不会回显任何值。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douweiluo0600 2017-02-02 16:10
    已采纳

    I think the argument is successfully being passed. The c code not using the argument. It is doing scanf to get user input and not really using the argument that is being passed. You need to change the c code to use the argument.

    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
        */ 
    }
    
    打赏 评论
  • dszdiavv474681 2017-02-02 05:28

    The c program is printing a new line in the below line

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

    In the php code, the comparison string is not including newline in the below line

    if($op=="H H")
    

    I suggest you change the php code to compare including newline (use strcmp to compare strings. Don't use == operator to compare strings)

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

    Hope this helps.

    打赏 评论

相关推荐 更多相似问题