• Is it possible to call a Command Procedure in fortran?

    From HCorte@21:1/5 to All on Wed Sep 1 08:38:57 2021
    Is it possible to call a command procedure in fortran? and if so obtain a variable/symbol of the command procedure.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to HCorte on Wed Sep 1 14:46:08 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to David Duffy on Wed Sep 1 17:04:26 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Duffy@21:1/5 to gah4@u.washington.edu on Wed Sep 1 23:53:06 2021
    gah4 <gah4@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John@21:1/5 to All on Wed Sep 1 18:08:12 2021
    On Wednesday, September 1, 2021 at 8:04:27 PM UTC-4, gah4 wrote:
    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.
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John@21:1/5 to All on Wed Sep 1 18:44:00 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John@21:1/5 to All on Wed Sep 1 18:19:02 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Louis Krupp@21:1/5 to HCorte on Wed Sep 1 21:58:28 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HCorte@21:1/5 to All on Thu Sep 2 03:29:15 2021
    A quinta-feira, 2 de setembro de 2021 à(s) 04:58:31 UTC+1, Louis Krupp escreveu:
    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

    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HCorte@21:1/5 to All on Thu Sep 2 04:17:49 2021
    A quinta-feira, 2 de setembro de 2021 à(s) 11:29:17 UTC+1, HCorte escreveu:
    A quinta-feira, 2 de setembro de 2021 à(s) 04:58:31 UTC+1, Louis Krupp escreveu:
    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
    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?

    $ FORTRAN /VERSION
    HP Fortran V8.2-104939-50H96

    $ SHOW SYSTEM
    OpenVMS V8.4

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From JCampbell@21:1/5 to HCorte on Thu Sep 2 18:30:58 2021
    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 ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dick Hendrickson@21:1/5 to JCampbell on Fri Sep 3 10:56:32 2021
    On 9/2/21 8:30 PM, JCampbell wrote:
    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 ?

    There is a WAIT option and some status arguments.

    Dick Hendrickson

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From abrsvc@21:1/5 to Dick Hendrickson on Sat Sep 11 04:43:28 2021
    On Friday, September 3, 2021 at 11:56:36 AM UTC-4, Dick Hendrickson wrote:
    On 9/2/21 8:30 PM, JCampbell wrote:
    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 ?

    There is a WAIT option and some status arguments.

    Dick Hendrickson

    When using an OpenVMS system, there is a library call: LIB$DO_COMMAND that can be used to execute command procedures.

    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)