EXECUTE_COMMAND_LINE(COMMAND, WAIT, EXITSTAT, CMDSTAT, CMDMSG) (Fortran 2008)

Purpose

Passes a command to the operating system for execution.

Class

Subroutine

Argument type and attributes

COMMAND
An INTENT(IN) CHARACTER scalar. It specifies the command line to be executed.
WAIT (optional)
An INTENT(IN) LOGICAL scalar. It determines whether COMMAND is executed synchronously or asynchronously.
  • If WAIT is set to .TRUE., COMMAND is executed synchronously.
  • If WAIT is set to .FALSE., COMMAND is executed asynchronously if that is supported, and synchronously otherwise.
  • If WAIT is not present, XL Fortran treats it as present with the value .TRUE.. Then COMMAND is executed synchronously.
Note: In the current release, XL Fortran supports only synchronous execution.
EXITSTAT (optional)
An INTENT(OUT) INTEGER scalar. If XL Fortran executes COMMAND synchronously, EXITSTAT is assigned the value of the system return code returned by the executed command when this argument is present. Otherwise, EXITSTAT remains unchanged.
CMDSTAT (optional)
An INTENT(OUT) INTEGER scalar. If CMDSTAT is present, it is assigned a value indicating the status of the command execution as follows:
  • If command-line execution is not supported, CMDSTAT is assigned the value of -1.
  • If no error occurs during the execution of COMMAND, and the value of WAIT is .FALSE., but asynchronous execution is not supported, CMDSTAT is assigned the value of -2.
  • If an error occurs, CMDSTAT is assigned the value of 1.
  • In all other cases, CMDSTAT is assigned the value of 0.
Note: If a condition occurs that would assign a nonzero value to CMDSTAT, but the CMDSTAT argument is not present, error termination is initiated.
CMDMSG (optional)
An INTENT(OUT) CHARACTER scalar. If an error occurs during the execution of COMMAND, CMDMSG is assigned the value of an explanatory message when this argument is present. Otherwise, CMDMSG remains unchanged.

Examples

CALL EXECUTE_COMMAND_LINE('rm -rf script')
OPEN(1, FILE = 'script')
CALL EXECUTE_COMMAND_LINE('date | awk \'{printf \" %s \", $1}\' >> script.out')
! Execute the 'cmd' command synchronously 
CHARACTER(LEN = 10) :: cmd = "du -s -m"
CALL EXECUTE_COMMAND_LINE(cmd, .TRUE.)
! Assign 'exit' the value of the system return code 
INTEGER :: exit = 0
CALL EXECUTE_COMMAND_LINE('/bin/mv mod1.mod mod2.mod', .TRUE., EXIT)
! Assign 'exit' the value of the system return code
! Assign 'cmdstat' the value of the status of the command execution
INTEGER :: exit, cmdstat = 0
CALL EXECUTE_COMMAND_LINE('echo running on = \'hostname\'', .TRUE., exit, cmdstat)
! If an error occurs, 'message' is assigned the value of 
! an explanatory message.

IMPLICIT NONE

CHARACTER(200) :: message = ""
INTEGER :: exit_s = 0, cmd_s = 0

CALL EXECUTE_COMMAND_LINE("mv f.1 f.2", EXITSTAT = exit_s, 
                           CMDSTAT = cmd_s, CMDMSG = message)

IF (cmd_s .NE. 0) THEN
  IF (cmd_s .EQ. -1) THEN
    PRINT *, "command execution not supported on this system"
    STOP 1
  END IF

  PRINT *, message
  STOP 2
END IF

IF (exit_s .NE. 0) THEN
  STOP 3
END IF

END

Related information

SYSTEM(CMD, RESULT) (IBM extension)

See the system subroutine in the AIX® Technical Reference: Base Operating System and Extensions Volume 2 for details about the underlying implementation.