LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2011, 05:02 AM   #1
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Rep: Reputation: Disabled
php problem with shell execution


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.

Last edited by nikhidet1; 12-05-2011 at 06:01 AM.
 
Old 12-05-2011, 05:46 AM   #2
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Hi,

If i understand this, the c program will not make any output but will create text.txt or text1.txt.

So why do you assign the non-output to the variable C in the shell script ?

And now in the php source why do you assign the output of a non output program to $output ?

When you say that it's not working what do you mean ?
 
Old 12-05-2011, 06:20 AM   #3
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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..

Last edited by nikhidet1; 12-05-2011 at 06:33 AM.
 
Old 12-05-2011, 11:18 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
You understand that there is security risk with :
PHP Code:
if(isset($_POST['arg1']))
{
$num1=$_POST['arg1'];
if(
$num1){
$output=shell_exec("./myfile $num1");
... 
I mean it is just for testing, right ?

Why using an external program for just open files ?
Also maybe there are permission issues when attempting to create a file with php user privileges ?
 
Old 12-07-2011, 03:41 AM   #5
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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

Last edited by nikhidet1; 12-08-2011 at 03:49 AM.
 
Old 12-07-2011, 05:12 AM   #6
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
just solve my problem..anyways thanks for all information
 
Old 12-07-2011, 08:20 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,731

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381
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.

BTW what are you trying to accomplish?

Last edited by michaelk; 12-07-2011 at 08:22 PM.
 
Old 12-08-2011, 03:41 AM   #8
nikhidet1
LQ Newbie
 
Registered: Oct 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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

Last edited by nikhidet1; 12-08-2011 at 03:50 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a shall script for remote shell execution ramesh14 Linux - Newbie 3 03-27-2011 08:59 AM
ELF execution in child shell. dina3e Programming 1 07-27-2008 11:17 PM
crond shell execution problems lm317t Linux - General 1 02-22-2005 10:46 AM
php command execution problem avswamy Programming 3 01-31-2005 05:55 AM
Speeding up Shell Script execution?? funkymunky Programming 8 07-16-2004 09:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration