当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Not able to pass arguments in shell_exec function

问题描述:

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\n", 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.

网友答案:

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
    */ 
}
网友答案:

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

printf("%c %c\n", 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\n") == 0)

Hope this helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: