Intel® Fortran Compiler 16.0 User and Reference Guide

EXECUTE_COMMAND_LINE

Intrinsic Subroutine: Executes a command line.

Syntax

CALL EXECUTE_COMMAND_LINE (command [,wait,exitstat,cmdstat,cmdmsg])

command

(Input) Must be a scalar of type default character. It is the command line to be executed. The interpretation is processor dependent.

wait

(Input; optional) Must be a scalar of type logical. If wait is present with the value FALSE and the processor supports asynchronous execution of the command, the command is executed asynchronously; otherwise it is executed synchronously.

exitstat

(Inout; optional) Must be a scalar of type integer. If the command is executed synchronously, exitstat is assigned the value of the processor-dependent exit status of the executed command. Otherwise, the value of exitstat is unchanged.

cmdstat

(Output; optional) Must be a scalar of type integer. It is assigned one of the following values:

  • -1 if the processor does not support command line execution

  • A processor-dependent positive value if an error condition occurs

  • -2 if no error condition occurs but wait is present with the value FALSE and the processor does not support asynchronous execution.

  • 0 otherwise

cmdmsg

(Inout; optional) Must be a scalar of type default character. If an error condition occurs, cmdmsg is assigned a processor-dependent explanatory message. Otherwise, cmdmsg is unchanged.

Description

When command is executed synchronously, EXECUTE_COMMAND_LINE returns after the command line has completed execution. Otherwise, EXECUTE_COMMAND_LINE returns without waiting.

If a condition occurs that would assign a nonzero value to cmdstat but cmdstat is not present, execution is terminated.

Note

If the application has a standard console window, command output will appear in that window. On Windows* OS, if the application does not have a console window, including QuickWin applications, command output will not be shown.

Example

INTEGER :: CSTAT, ESTAT
CHARACTER(100) :: CMSG
CALL EXECUTE_COMMAND_LINE (“dir > dir.txt”, EXITSTAT=ESTAT, &
           CMDSTAT=CSTAT, CMDMSG=CMSG)
IF (CSTAT > 0) THEN
  PRINT *, “Command execution failed with error “, TRIM(CMSG)
ELSE IF (CSTAT < 0) THEN
  PRINT *, “Command execution not supported”
ELSE
  PRINT *, “Command completed with status “, ESTAT
END IF
END

In the above example, EXECUTE_COMMAND_LINE is called to execute a "dir" command with output redirected to a file. Since the WAIT argument was omitted, the call waits for the command to complete and the command's exit status is returned in ESTAT. If the command cannot be executed, the error message returned in CMSG is displayed, but note that this is not based on the success or failure of the command itself.