skip to Main Content

I have three files. First file contains commands which are needed to be executed and this file is .txt file. Second file is output.txt file which stores the output of the command which has been executed. Third file is C program through which i will execute the commands from the first file. Now i have tried three methods which are available in c programming which are popen(), system() and execl() functions.

  1. when i used popen() method it has worked fine, but for every command it is creating child process, i dont want that, for every command it should be executed in single process which is parent process and i just executed basics command like pwd and cd and my output is as follows:

COMMAND: pwd
OUTPUT: /home/basavaraj/Desktop/My_Folders/C

COMMAND: cd /home/basavaraj/Documents && pwd
OUTPUT: /home/basavaraj/Documents

COMMAND: pwd
OUTPUT: /home/basavaraj/Desktop/My_Folders/C

So, this is my output for those commands when i used cd and pwd in single command line, i got confirmed that child process is being created.
This is what i don’t want to happen, for every command it should execute in parent process.

  1. For system() function it is returning an integer, i want output which is string to be stored in output.txt file, so what should i do to store the output in .txt file.

  2. For execl() function it is running only one command after executing one command it is terminating the whole C program, i want to execute each and every command from command text file, so what should be done to execute every command from command file.

Please help me with this, i’m stuck at every method, please explain what and how to do it, please explain in your comfort method and is there any other method’s which i’m not aware, please feel free to tell about that.

EDIT: The source of this C-program:

while (fgets(command, bufferSize, input_file) != NULL)      
{         
    fprintf(shellPipe, "%s", command);                  
    while (fgets(output_buffer, sizeof(output_buffer), shellPipe) != NULL)          
       {         
           fprintf(output_file, "%s", output_buffer);         
       }      
}

2

Answers


  1. Chosen as BEST ANSWER
    int main() 
    { 
        const char* shellCommand = "/bin/sh";
        const char* shellArgs = "-c";
        char command[500];
    
        FILE * input_file = fopen("command_list.txt", "r");
        FILE * output_file = fopen("output.txt","w");
        FILE* shellPipe = popen(shellCommand, "w");
    
        char buffer[300] ;
        while (fgets(command, sizeof(command), input_file) != NULL) 
        {
         fprintf(output_file, "COMMAND: %s",command);
         fprintf(output_file, "OUTPUT: n");
         fprintf(shellPipe, "%s", command); 
    
            while(fgets(buffer,sizeof(buffer),shellPipe) != NULL)
            {
                fprintf(output_file,"%s",buffer);
            }  
           
        }
        pclose(shellPipe);
        fclose(input_file);
        fclose(output_file);
        return 0;
    }
    

    I'm facing a problem here, the output is printing in terminal which is supposed to print in output_file,I have tried to use buffer

     while(fgets(buffer,sizeof(buffer),shellPipe) != NULL)
            {
                fprintf(output_file,"%s",buffer);
            } 
    

    to print but it's not printing in output file, where i'm going wrong please help me with this problem.


  2. Assume commands.txt is the input file with commands, output.txt is the file that stores the output. As your commands must be executed by the shell anyway (otherwise you would have to re-implement the shell, which you probably are not planning to do), the simplest method to achieve what you want is:

    system("/bin/sh <commands.txt >output.txt");
    

    However, if you want to do something more with the output from commands than just write it to a file (eg. process it somehow in your program), you can use something like this:

    #include <stdio.h>
    
    int main()
    {
      FILE *inputfile, *outputfile;
    
      char line[500];
    
      inputfile = popen("/bin/sh <commands.txt", "r");
      outputfile = fopen("output.txt", "w");
    
      while ( fgets( line, sizeof line, inputfile))
      {
        fprintf(outputfile, "%s", line);
      }
    
      pclose(inputfile);
      fclose(outputfile);
    }
    

    Also, if you can make your commands.txt file executable (for example by using chmod() function in your program, if the user running program is also the owner of the file), you can replace the popen("/bin/sh <commands.txt", "r") call in above code by just popen("./commands.txt", "r") – everything else remains the same.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search