Command-Line Execution

The execute_command_line() runs a shell command, either synchronously or asynchronously. The routine was introduced in Fortran 2008 and replaces the non-standard system(). The output of executed programs is not recorded, and must be piped to and read from file, if necessary (see cURL for an example).

call execute_command_line(command[, wait][, exitstat][, cmdstat][, cmdmsg])
Argument Type Description
command character Command to execute.
wait logical If .false., given command is run asynchronously (optional).
exitstat integer Exit code of the executed command (optional).
cmdstat integer Will be set to 0 if command was executed successfully (optional).
cmdmsg character Error message, if an error occured (optional).

The following example calls FIGlet to turn a string into a text banner. On FreeBSD, install FIGlet with:

# pkg install misc/figlet

Commands are run synchronously by default, i. e., the Fortran program will not continue until the executed program terminates:

! example.f90
program main
    implicit none
    character(len=*), parameter :: MESSAGE = 'FORTRAN'

    call execute_command_line('figlet -f banner "' // MESSAGE // '"')
end program main

Compile and execute the program with:

$ gfortran13 -o example example.f90
$ ./example
####### ####### ######  ####### ######     #    #     #
#       #     # #     #    #    #     #   # #   ##    #
#       #     # #     #    #    #     #  #   #  # #   #
#####   #     # ######     #    ######  #     # #  #  #
#       #     # #   #      #    #   #   ####### #   # #
#       #     # #    #     #    #    #  #     # #    ##
#       ####### #     #    #    #     # #     # #     #

A more useful program we can execute is Xdialog, a command-line tool to display message boxes and graphical dialogs on X11, for example, to inform the user that an arbitrary task has been finished:

! dialog.f90
program main
    ! ...
    ! long-running computations here
    ! ...

    call execute_command_line('Xdialog --msgbox "The job is done." 200x100')
end program main