Programming in Modern Fortran
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:
$ gfortran10 -o example example.f90
$ ./example
####### ####### ###### ####### ###### # # #
# # # # # # # # # # ## #
# # # # # # # # # # # # #
##### # # ###### # ###### # # # # #
# # # # # # # # ####### # # #
# # # # # # # # # # # ##
# ####### # # # # # # # # #
< Environment Variables | [Index] | Inter-Process Communication > |