HOWTO AIX:TO INVOKE SHELL SCRIPTS IN C

ITEM: RTA000015300



  *********************************************************************         
  ** This entry contains information that is intended to be helpful, **         
  ** accurate and act as a guide of HOW TO accomplish a given system **         
  ** task.  The provided explanations, techniques and procedures     **         
  ** have been reviewed for technical accuracy and applicability,    **         
  ** but have not been tested in every possible environment or       **         
  ** situation.                                                      **         
  **                                                                 **         
  **   Normal precautions should be taken in adopting these same     **         
  **   techniques and procedures, because as product and system      **         
  **   interfaces change, so would the usage of this information     **         
  **   change.                                                       **         
  *********************************************************************         
                                                                                
  SCENARIO/EXPLANATION:                                                        
                                                                                
  SOLUTION:                                                                     
                                                                                
  1. Approaches                                                                 
                                                                                
  There are several approaches to invoke a shell script from a 'C'              
  program; each of these approaches is associated with one or more of           
  unique system calls and subroutines. Because of the changes in the            
  programming environment, the optimum solution to invoke a shell               
  script from a 'C' program varies from one application to another.             
                                                                                
  Consider the following available options while selecting an approach :        
                                                                                
  a. The process which executes a 'C' program, can be programmed to             
  create a child process that in turn executes the shell script. Upon          
  the completion of executing the shell script, the original process            
  continues to execute the rest of the program. However, a process can          
  also can be programmed to invoke a shell script from calling 'C'              
  program, without creating a child process. But the same process will          
  never return to the calling 'C' program again.                                
                                                                                
  b. System calls and subroutines which are invoked from the 'C' program,       
  can pass parameters that point to null terminated character strings.          
  These strings are passed as the arguments to the shell script. System         
  calls and subroutines can also pass a parameter, which is an array            
  of pointers, pointing to null terminated character strings.                   
                                                                                
  c. The shell script will inherit the same system environment as the           
  calling 'C' program.                                                          
                                                                               
  2. The system call and the subroutines                                        
                                                                                
  The system calls and subroutines that are included in this article are        
  : 'system', 'execlp', and 'execvp'.                                           
                                                                                
  The 'system' subroutine invokes the 'fork' system call, to create a           
  child process that in turn uses 'exec' system call to run /bin/sh. Upon       
  the completion of this child process, the 'system' subroutine returns         
  the exit status of the shell to the calling program. For the purpose          
  of this article, the calling 'C' program is to invoke a shell script,         
  and return a value to the calling 'C' program after the execution.            
                                                                                
  The Syntax of this subroutine is :                                            
                                                                                
      #include                                                        
      int system (string)                                                       
      char *string;                                                             
                                                                                
      where 'string' is a executable 'sh' command. for the purpose in           
      this article, it is the name of a shell script.                           
                                                                                
  The 'execlp' and the 'execvp' system calls execute a new program in           
  the calling process. They do not create a new process, but overlap the        
  calling program with the called new program. Upon successful                  
  completion, these system calls do not return to the calling program.          
  For the purpose of this article, the calling 'C' program invokes a            
  shell script, and executes this script by the original process.               
                                                                                
  The syntax for these system calls are :                                       
                                                                               
      include                                                          
      int execlp (file, arg0, arg1, ...argn, 0)                                 
      char *file, *arg0, *arg1,...;                                             
                                                                                
      int execvp (file, argv)                                                   
      char *file, *argv< >;                                                     
                                                                                
      Where :                                                                   
      < > is the left block and the right block symbol in "*argv< >".           
      The 'file' is the name of a executable file.                              
      The arg0, arg1 ... point to null terminated character string.             
      The argv is an array of pointers to null terminated string.               
                                                                                
  The following examples are programs, which invoke the related system          
  calls and subroutines to perform specific functions. These programs          
  have been tested on the RT/AIX platform only. A few changes might be          
  required before applying these programs to another AIX platform.              
                                                                                
  3. Examples                                                                   
                                                                                
  Example 1 :                                                                   
                                                                                
  In this program, the 'system' subroutine is to create a child process,        
  and execute a shell script in this child process.                             
                                                                                
      # include                                                        
      main()                                                                    
                                                                                
      {                                                                         
        printf("in C program, before system call\n");                          
        if (system("script1 is in executing") == -1)                            
          printf("system call failed\n");                                       
        printf("in C program, after system call\n");                            
                                                                                
      }                                                                         
  The script file 'script1' might look like this :                              
                                                                                
      # script1, print file name first                                          
      #                                                                         
      echo $0 $*                                                                
                                                                                
  If the 'system' call succeeds, the output of this program will be :           
                                                                                
      in C program, before system call                                          
      script1 is in executing                                                  
      in C program, after system call                                           
                                                                                
  Example 2 :                                                                   
                                                                                
  In this program, the 'execlp' system call is to execute a shell               
  script. The "in C program, after execlp call" should never be printed         
  if the 'execlp' system call succeeds. Note, the process will search           
  the script file using the 'PATH' environment variable.                        
                                                                                
      # include                                                        
      main(argc, argv)                                                          
      int argc;                                                                 
      char **argv;                                                              
                                                                                
      {                                                                        
        printf("in C program, before execlp call\n");                           
        if (execlp("script1", "is", "in", "executing",0) < 0)                   
          perror(argv<0>), exit(1);                                             
        printf("in C program, after execlp call\n");                            
                                                                                
      }                                                                         
  The script file 'script1' might look like this :                              
                                                                                
      # script1, print file name first                                          
      #                                                                         
      echo $0 $*                                                                
                                                                                
  If 'execlp' call succeeds, the output will be :                               
                                                                                
      in C program, before execlp call                                         
      script1 is in executing                                                   
                                                                                
  Example 3 :                                                                   
                                                                                
  In this program, the 'execvp' system call is to execute shell script.         
  The "in C program, after execvp call" should never be printed if              
  the 'execvp' system call succeeds. Note the process will search the           
  script file using the 'PATH' environment variable.                            
                                                                                
      /* Object file name is assigned to be "execvp.howto". */                  
      # include                                                        
      main(argc, argv)                                                          
      int argc;                                                                 
                                                                                
      char **argv;                                                             
                                                                                
      {                                                                         
        printf("in C program, before execvp call\n");                           
        if (execvp("script1", argv) < 0)                                        
          perror(argv<0>), exit(1);                                             
        printf("in C program, after execvp call\n");                            
                                                                                
       }                                                                        
  The script file 'script1' might look like this :                              
                                                                                
      # script1, print file name first                                          
      #                                                                         
      echo $0 $*                                                                
                                                                                
  Executing this program with the shell command "execvp.howto is in            
  executing". If 'execvp' call succeeds, the output will be :                   
                                                                                
      in C program, before execvp call                                          
      script1 is in executing                                                   
                                                                                
  REFERENCES:                                                                   
                   RT  PC  AIX  :  Technical Reference, System Calls and        
                 Subroutines.                                                   
                   RT PC AIX  Language Guide and Reference.                     
                   IBM AIX C Language Reference, SC23-2058-0.                   
                                                                                
 ******************************************************************        
 ** If you have found this information to be informative and     **        
 ** useful, please let us know via a HONE FEEDBACK or via EQUAL. **        
 ** Make sure in your evaluation to reference the appropriate    **       
 ** INFOSYS OR FLASH NUMBER                                      **        
 **                                                              **        
 **    We would like to develop as many HOWTO entries as needed  **        
 **   and solicit your suggestions, comments and recommendations **        
 **    on how to make these entries more useful.                 **        
 **                                                              **        
 **  WE NEED TO KNOW THE VALUE OF THIS INFORMATION TO DETERMINE  **        
 **   WHETHER IT IS WORTH IBM TIME AND RESOURCE TO CONTINUE TO   **        
 **         CREATE AND MAINTAIN 'HOWTO' INFOSYS ENTRIES.         **        
 ******************************************************************        
                                                                                
---------- ---------- ---------- --------- ---------- ----------                
                                                                                
                                                                                
This item was created from library item Q486222      4VLCH                     
                                                                                
Additional search words:                                                        
AIX AIXRT AIXRTL ALTERNATE COMPILERS HOWTO INDEX INVOCATION INVOKE              
IX JUL91 PROGRAM SCRIPTS SHELL SOFTWARE 4VLCH                                   
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                               


WWQA: ITEM: RTA000015300 ITEM: RTA000015300
Dated: 01/1996 Category: RISCO
This HTML file was generated 99/06/24~12:43:07
Comments or suggestions? Contact us