Find the answer to your Linux question:
Results 1 to 9 of 9
hey guys, i am interested in using shell_exec but somehow it doesn't give me the output like i want. Let me explain a little bit about what i did and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34

    shell_exec() in php..is the usage similar to shell script


    hey guys,
    i am interested in using shell_exec but somehow it doesn't give me the output like i want.
    Let me explain a little bit about what i did and what i am interested to do.
    I have created a shell script which can execute a c program and this c program will create a text file according to some conditions.

    so this is my shell script and i named it as test.sh:

    echo -n "enter the value to pass to c programm : "
    read var
    echo "The value is : $var"
    $(./myfile $var)

    and below is my 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";
    printf("option:%d\n",option);
    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 compiled my c program using gcc -o myfile myfile.c as well as compile my shell script using chmod +x test.sh.then i executed my shell script using ./test.sh.Then it will ask me to give the value to my variable var.if i enter whatever value as the value of var,this value will be later passed to c program as the main argument.i convert the main argument argv[1] to integer using atoi(argv[1]).so by doing this i will give value to the variable option.then i used the if statements and provided conditions to them.if the condition true,the txt file will be created.for example i give the value of 1 to var in my shell script, and this value of 1 will be pass to argv[1].as argv is converted to integer,the option now will posses the value of 1.As option equal to 1 the 1st if statement will evaluate option==1 as true and as a result text.txt will be created.Otherwise if i enter 2,the text1.txt will be created

    Using the same idea, i plan to used my php to do the same as shell script.
    This is my php code
    <html>
    <title>File Option</title>
    <body>
    <h1>Give a suitable value</h1>
    1:File_One
    2:File_Two
    <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=system("./myfile $num1");
    print "<p>$output</p>";
    }
    }
    ?>
    </body>
    </html>
    So basically the idea is if i enter 1 in the input and click GO,this value of 1 will pass to the main argument using the command shell_execution(./myfile $num1) and the text.txt will be created.unfortunately this text.txt has not created..i dont know why and thats why i decided to seek some ideas here..thanks in advanced

  2. #2
    Just Joined!
    Join Date
    Feb 2008
    Posts
    14
    Quote Originally Posted by nikhidet View Post
    hey guys,
    i am interested in using shell_exec but somehow it doesn't give me the output like i want.
    Let me explain a little bit about what i did and what i am interested to do.
    I have created a shell script which can execute a c program and this c program will create a text file according to some conditions.

    so this is my shell script and i named it as test.sh:

    echo -n "enter the value to pass to c programm : "
    read var
    echo "The value is : $var"
    $(./myfile $var)

    and below is my 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";
    printf("option:%d\n",option);
    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 compiled my c program using gcc -o myfile myfile.c as well as compile my shell script using chmod +x test.sh.then i executed my shell script using ./test.sh.Then it will ask me to give the value to my variable var.if i enter whatever value as the value of var,this value will be later passed to c program as the main argument.i convert the main argument argv[1] to integer using atoi(argv[1]).so by doing this i will give value to the variable option.then i used the if statements and provided conditions to them.if the condition true,the txt file will be created.for example i give the value of 1 to var in my shell script, and this value of 1 will be pass to argv[1].as argv is converted to integer,the option now will posses the value of 1.As option equal to 1 the 1st if statement will evaluate option==1 as true and as a result text.txt will be created.Otherwise if i enter 2,the text1.txt will be created

    Using the same idea, i plan to used my php to do the same as shell script.
    This is my php code
    <html>
    <title>File Option</title>
    <body>
    <h1>Give a suitable value</h1>
    1:File_One
    2:File_Two
    <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=system("./myfile $num1");
    print "<p>$output</p>";
    }
    }
    ?>
    </body>
    </html>
    So basically the idea is if i enter 1 in the input and click GO,this value of 1 will pass to the main argument using the command shell_execution(./myfile $num1) and the text.txt will be created.unfortunately this text.txt has not created..i dont know why and thats why i decided to seek some ideas here..thanks in advanced
    You don't need C to write to files you can do this all inside of PHP.
    If you give me a better idea of what your attempting to do I can write you a sample file.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    13
    What you are trying to do is a cgi-bin type of operation which most Apache/PHP servers will not allow you to create files in a local directory unless you are running suPHP which does allow you to do it. You may be able to put your files into a temp directory that your Apache/PHP server permits but then you'd need to go and fetch them from there. The better solution is to do your work in PHP which will obviate the need to for you to do temp file localization of cgi-bin executables (your compiled C program) but if you insist go learn about how to use cgi-bin on your server and see if your version of PHP will allow putting files in the locations you want them. Mine wouldn't so I had to change hosts to a suPHP server. YMMV

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    14
    Quote Originally Posted by aot2002 View Post
    You don't need C to write to files you can do this all inside of PHP.
    If you give me a better idea of what your attempting to do I can write you a sample file.
    Might be a permissions issue.

    Create a directory inside of your executing directory and chmod 777 on that directory

    mkdir UPLOAD
    chmod 777 UPLOAD

    Then allow your script to write the files in UPLOAD.

    Also might be a current directory issue.
    PHP is shell executing so you might be shelling out to a different location when you run the c program.
    most of the time it's the user's home directory that it defaults to when you shell out.
    Either specify full path or change the directory to match where the file sits.
    Just because the files sit in the same directory does not mean that you shell out to the same directory.

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    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 it will create another text file after execution.for example i have a text file containing hexadecimals and the other text file containing also hexadecimals and descriptions named as Description1.txt.so i compared these hexadecimals in this two text files and after comparing them if these hexadecimal is similar in both text file,both of hexadecimal and description will be written in another text file.i succeeded in doing this in c program.There are 3 text files containing hexadecimal as well as description.Lets i named 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 script and i used (switch case statement in c program) to indicate which text file should be compared when i pass the value of 1,2 or 3 from shell script .if i enter 1 in the shell program,the case 1 will be performed and the program will compared text file containing hexadecimal and the text file named Description1.txt .i succeeded in doing this.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 give have any more ideas please give me some information...thanks in advanced

  6. #6
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    Quote Originally Posted by aot2002 View Post
    Might be a permissions issue.

    Create a directory inside of your executing directory and chmod 777 on that directory

    mkdir UPLOAD
    chmod 777 UPLOAD

    Then allow your script to write the files in UPLOAD.

    Also might be a current directory issue. .
    could u give me an example how to allow your script to write files in UPLOAD?i coulnt understand this part...thanks in advanced.

  7. #7
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    hey
    i have succeeded in doing it...thanks for all helps and information.really appreciate it...it just a problem of permission...anyways thanks..

  8. #8
    Just Joined!
    Join Date
    Feb 2008
    Posts
    14
    Quote Originally Posted by nikhidet View Post
    hey
    i have succeeded in doing it...thanks for all helps and information.really appreciate it...it just a problem of permission...anyways thanks..
    Congrats, also just so you know you don't have to go out to the shell with php you can write and compare files using md5 in php after you write them.

    PHP: md5

    <?php

    $file = file_get_contents('./test1.txt', true);
    $file2 = file_get_contents('./test2.txt', true);

    if (md5($file) === md5($file2)) {
    echo "Files match";
    }else{
    echo "Files don't match";
    }
    ?>

  9. #9
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    thanks for the codes.i will try it later..anyway thanks for all those helps and info..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •