Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to call a Command Procedure in fortran?

201 views
Skip to first unread message

HCorte

unread,
Sep 1, 2021, 11:38:58 AM9/1/21
to
Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.

gah4

unread,
Sep 1, 2021, 5:46:10 PM9/1/21
to
On Wednesday, September 1, 2021 at 8:38:58 AM UTC-7, HCorte wrote:
> Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.

There is EXECUTE_COMMAND_LINE which will execute anything that is normally executable.

In the usual implementation on unix-like systems it calls sh to run the program,
and on windows it calls CMD.EXE to run it. Unix rule is that programs run by a shell can't
change environment variables in the host shell. There are funny tricks used by programs
that do need to do that.

As well as I know it, CMD doesn't have that restriction when running CMD files.
(Traditionally CMD and BAT files work the same way.)
However, EXECUTE_COMMAND_LINE itself doesn't have a way to get variables back.
You could write a CMD file that would then run the one you want, and pass back any
variables in an appropriate way.

David Duffy

unread,
Sep 1, 2021, 7:53:09 PM9/1/21
to
gah4 <ga...@u.washington.edu> wrote:
> On Wednesday, September 1, 2021 at 8:38:58 AM UTC-7, HCorte wrote:
>> Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.
>
> There is EXECUTE_COMMAND_LINE which will execute anything that is normally executable.
>
Most generally, you will have to then capture the output of the command to a file, and
read that back in. You can using C interop to directly access such
commands, but this is going to be system specific.o
Cheers, David Duffy.

gah4

unread,
Sep 1, 2021, 8:04:27 PM9/1/21
to
On Wednesday, September 1, 2021 at 4:53:09 PM UTC-7, David Duffy wrote:

(snip)

> Most generally, you will have to then capture the output of the command to a file, and
> read that back in. You can using C interop to directly access such
> commands, but this is going to be system specific.o
> Cheers, David Duffy.

Well if you are doing that, might as well call popen().
(But yes, it will still be system specific.)

Though it would be better to have a real Fortran equivalent to popen().

In the Fortran 77 days, with HP-UX, I did manage to use popen().
HP-UX Fortran (at least used to) have a way to connect a Unix file
descriptor to a Fortran unit. I had a program do the popen() and then
return the fd, for the Fortran program to use. That was so I could
spool directly to lpr.

John

unread,
Sep 1, 2021, 9:08:14 PM9/1/21
to
As mentioned, you can execute a system command:
https://urbanjost.github.io/M_intrinsics/execute_command_line.3fortran.html
and if you are on a POSIX system this should work for calling popen and returning the
output into a character array
https://github.com/urbanjost/M_process
or can be used as an example on how to do that. That would not work on MSWIndows unless you were in CygWin or WSL (WIndows Subsystem for Linux) or something else that provided a POSIX interface. I think modern MSWIndows might have a procedure called _popen that is similar, but not all that familiar with it. It is more portable to call execute_command_line(3f) and write data into a file, as explained above but if you are a POSIX/Unix/GNU Linux user the popen(3c) interface is very versatile and easy to use via an interface like M_process(3f). I didn't really get from the post just what kind of information you wanted to get back into the program.

John

unread,
Sep 1, 2021, 9:19:04 PM9/1/21
to
PS: An often-overlooked way to get data back into your Fortran program from a system command is to have the system command write a NAMELIST file, which can then be read with a single READ from your program, which can eliminate having to parse the character output from the POPEN interface used by something like M_process. That is another reason to call a command via EXECUTE_COMMAND_LINE(3f) and read the data back in, as the READ statement can do the type conversions too if your command writes something easy to read.

John

unread,
Sep 1, 2021, 9:44:02 PM9/1/21
to
program getstuff
! put the values you want to get back in a namelist
! like the date and hostname in this example
character(len=80) :: date ;namelist /systemstuff/ date
character(len=80) :: host ;namelist /systemstuff/ host
integer :: lun
! assumes a POSIX system with the sh|bash command
! call a system command that writes a little NAMELIST group
call execute_command_line('echo "&SYSTEMSTUFF DATE=''`date`'', HOST=''`hostname`'' /"> junko')
! read the NAMELIST file and delete it
open(newunit=lun,file='junko') ! should generate a more unique name and check IOSTAT and such
read(lun,nml=systemstuff)
! now back to normal Fortran
write(*,*)'The date is ',trim(date)
write(*,*)'The hostname is ',trim(host)
close(lun,status='delete')
end program getstuff

works with at least three compilers on a Linux system

ifort x..f90
./a.out
The date is Wed 01 Sep 2021 09:34:23 PM EDT
The hostname is venus

nvfortran x..f90
./a.out
The date is Wed 01 Sep 2021 09:34:33 PM EDT
The hostname is venus

gfortran x..f90
./a.out
The date is Wed 01 Sep 2021 09:34:45 PM EDT
The hostname is venus

Louis Krupp

unread,
Sep 1, 2021, 11:58:31 PM9/1/21
to
On 9/1/2021 9:38 AM, HCorte wrote:
> Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.
Are you by any chance using OpenVMS?

Louis

HCorte

unread,
Sep 2, 2021, 6:29:17 AM9/2/21
to
Yes Louis using OpenVMS sorry didn't provide the OS before, is EXECUTE_COMMAND_LINE specific to Unix,Linux or does it also work in OpenVMS?

HCorte

unread,
Sep 2, 2021, 7:17:51 AM9/2/21
to
$ FORTRAN /VERSION
HP Fortran V8.2-104939-50H96

$ SHOW SYSTEM
OpenVMS V8.4

JCampbell

unread,
Sep 2, 2021, 9:31:00 PM9/2/21
to
On Thursday, September 2, 2021 at 1:38:58 AM UTC+10, HCorte wrote:
> Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.
If you are recovering information from the external procedure, isn't the important part of the question how to execute an external procedure then wait for it to complete before proceeding?

You need to know when the results of the "command procedure" are available before proceeding.
This can be the messy part, such as testing for a file to be created, unless this is an option in EXECUTE_COMMAND_LINE ?

Dick Hendrickson

unread,
Sep 3, 2021, 11:56:36 AM9/3/21
to
There is a WAIT option and some status arguments.

Dick Hendrickson

abrsvc

unread,
Sep 11, 2021, 7:43:30 AM9/11/21
to
When using an OpenVMS system, there is a library call: LIB$DO_COMMAND that can be used to execute command procedures.

Dan
0 new messages