ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hey
i learned that c programm can be executed using shell script..i have done it and it is working well.but now im trying to do similar thing using shell execution in php.but i got a problem.
this is what im trying to do
for example
i have this c program
#include <stdio.h>
#include <stdlib.h>
int main(int arg,char *argv[])
{
int option=atoi(argv[1]);
int i=0;
int d[10]={1,2,3,4,5,6,7,8,9,10};
FILE *fp;
char file_one[]="text.txt";
char file_second[]="text1.txt";
if(option==1)
{
fp=fopen(file_one,"a");
if(fp==NULL) exit(1);
}
if(option==2)
{
fp=fopen(file_second,"a");
if(fp==NULL) exit(1);
}
for(i=0;i<10;i++){
fprintf(fp,"x[%d]:%d,",i,d[i]);
}
return 0;
}
I can execute this c program and passing the value to it using this shell script:
#!/bin/sh
echo -n "enter the value to pass to c programm : "
read var
echo "The value is : $var"
c=$(./myfile $var)
As a result,this shell script will execute c program and then text.txt will be presented in my directory if var=1 or text1.txt if var=2.
After that i tried to do similar things using php
this is my code:
<html>
<body>
<form method="post">
<p>Num1:<input type="text" name="arg1" size="40" value=""/></p>
<p><input type="submit" name="submit" value="GO"/>
</form>
<?php
if(isset($_POST['arg1']))
{
$num1=$_POST['arg1'];
if($num1){
$output=shell_exec("./myfile $num1");
print "<p>$output</p>";
}
}
?>
</body>
</html>
But it doesnt give me the same result as shell script.why?did i do something wrong in my php code?
is the command shell_exec("./myfile $num1") similar to $(./myfile $my_var)?i assumed that these both commands are similar.am i wrong?hope someone can guide me to make my program work.thanks in advanced.
oh basically this is what i am planning to do. ok yeah u r right.my c program will create text file if my variable option equivalent to 1 or 2.By using shell script i will pass the value of var to variable "option" using main argument.Then i convert this main argument to integer type using atoi(argv[1]).As a result, if i passed var=1 to the c program,the "option" will be equaled to 1 and then the if condition will evaluate option==1 as true and the text.txt will be created.otherwise if the passing value of var=2,the variable option will be equaled to 2 and the text1.txt will be created.about the c variable in shell script we can remove it because it doesnt have any specific meaning.so instead of c=$(./myfile var) i can write it as $(./myfile var).the "output" in php was used in my previous program and i forgot to remove it.we can remove it and replace it as shell_exec('./myfile $num1');
after i compile this shell script using chmod +x try.sh and execute it using ./try.sh,it will ask me to enter the value of var ("enter the value to pass to c programm : ").if i enter 1,1 will be passed to variable option in the c program and the if condition evaluate option==1 as true and it will create the text.txt.if i enter 2,the option==2 is evaluated as true and it will create the text1.txt.
I want my php to work like my shell script.but till now php source still doesnt have similar output as shell script.for instance,i give the value for $num1 on the web page equal to 1 and this value will be passed to the main argument through the command shell_exec(./myfile $num1).unfortunately when i click the button GO,the text.txt is not created in my directory even though the variable "option" yet is equivalent to 1..that's what i mean by it is not working.hopefully you can understand my explanation..
erm actually i am not really good in php..i have just started to learn it 2 months ago..basically i knew only part of it...i am not just trying to open file as my interest is not only opening the file...i have just created a c program program that compare text files and then after executed this c program will create another text file.for example i have a text file containing hexadecimal and the other text file containing also hexadecimals and descriptions called as Description1.txt.so i compared the hexadecimals in this two text files and created another text file.i succeeded in doing this in c program.There are 3 text files containing hexadecimal as well as description.Lets i name these 3 files as Description1.txt,Description2.txt and Description3.txt. so in my c program i changed a little bit of the code so that i can pass a value to my main parameter using shell.As i tried this with shell script and succeeded,i wanted to do the same with shell exec in php.but it is not working.i even tried to put my c program code as php code...it still not gave me the result that i wished to have...anyways thanks for information...hopefully if u have any more ideas please give me some information...thanks in advanced
FYI The . in ./myfile is a shortcut for current working directory. The path environment can displayed using the echo command i.e.
echo $PATH. So any program that is located in the those directories can be executed just by typing in its name. If not then you have to include the path.
i have accomplished what i wished to have..erm basically i want my php to run a c program that after executed,the text file will be created and will be placed in my /var/www directory..anyway i have done it..previously there was nothing created after i executed my php.this was due to the permission issue..but now i have solved it..thanks for the great help and the information from u guys
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.