Asked  8 Months ago    Answers:  5   Viewed   11 times

Is there a way to store the output of a command line utility to a variable in Fortran?

I have a BASH based utility which gives me a number which needs to be used in a Fortran program. I want to call the utility through the program itself, and avoid writing the output to a file if possible.

Something like this maybe?

integer a
write(a,*) call execute_command_line('echo 5')

Or like this maybe?

read(call execute_command_line('echo 5'),*) a

I don't think either of these is right though. I would like to know if there is actually a method to do this. I read the docs for execute_command_line but I don't think there is an output argument for the subroutine which does this.

 Answers

3

Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like

program readfifo
  implicit none
  integer :: u, i
  logical :: ex
  inquire(exist=ex, file='foo')
  if (.not. ex) then
     call execute_command_line ("mkfifo foo")
  end if
  call execute_command_line ("echo 5 > foo&")
  open(newunit=u, file='foo', action='read')
  read(u, *) i
  write(*, *) 'Managed to read the value ', i
end program readfifo

Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).

Saturday, October 2, 2021
 
sgldiv
 
4

Use this command line instead:

C:WindowsSystem32Java.exe -version 2> C:Userstxt.txt

(note the additional 2.)

Java is writing the version information to standard error (channel 2) rather than standard output.

Wednesday, August 4, 2021
 
ariel
 
1

If your gfortran was installed a long time ago and you have updated macOS since installing, it may need re-installing to get correctly aligned and linked with the latest macOS tools and libraries.

My advice would be to:

  • uninstall gfortran,
  • check that Xcode and its command line tools are up-to-date,
  • re-install gfortran.

Hints for each of those steps follow:

Note that gfortran is a part of GCC - the "GNU Compiler Collection".

If you installed gfortran via homebrew, you can remove it with:

brew rm gcc

You can update Xcode by by going to AppStore and clicking Updates at top-right.

The Xcode Command Line tools include make and git and command-line versions of the compilers. You can install/update the Xcode command line tools with:

xcode-select --install

You can install gfortran with homebrew using:

brew install gcc

When you are finished, you should make sure that your PATH includes /usr/local/bin near the start and that there are no errors when you run:

brew doctor

which is a brilliant utility that checks your homebrew configuration is correct.

Friday, August 6, 2021
 
ariel
 
3

Fortran 90 is dead. It had serious deficiencies, especially with allocatables, that were fixed by Fortran 95. For example, it allowed existence of undefined allocation ststus.

Many times someone says he is using Fortran 90 he is in fact using something else.

Fortran 90 and Fortran 95 are so close that there is even no option to enable Fortran 90 in compilers. It would mostly just enable those errors and oversights in the standard and disable just a few features.

Gfortran, in the default mode gnu is actually close to a Fortran 2003 or 2008 compiler. Some of the default settings are different from Fortran 90 and 95. For example the automatic re-allocation on assignment. But it is normally good for programs in Fortran 90 and 95 and even some in 77. It enables certain common extensions to the standard. Certsin less common extensions can be enabled by special flags.

There is no reason to worry. Fortran 95 is the standard people should care about. Fortran 90 is not.

Regarding the .for extension, the compiler should process correctly if it is conforming fixed form source, be it 77, 90, 95, 2008, whatever.

Saturday, August 7, 2021
 
2

You will want to specify the encoding of your string before you print it:

print unicode(hex(self.bb.bb.start + self.offset)).encode('utf-8')
print unicode(self.pos_instruction, self.ins.get_name()).encode('utf-8')
print unicode(self.ins.show_buff( self.bb.bb.start + self.offset )).encode('utf-8')

The reason this works is because python automatically encodes your string correctly (in your case utf-8) when printing to the terminal (it detects that the terminal uses utf-8).

When you are redirecting your output to a file instead, Python has no information about what encoding it should use and it defaults to ascii instead (which is causing your error).

As a general rule of thumb, make sure you always encode your string before printing to make print work in all environments.

The best method may be to define your own print method for this:

def myprint(unicodestr): 
    print unicodestr.encode('utf-8')

If you want to avoid the above and make printing with utf-8 encoding the default you can do

import sys
import codecs
sys.stdout=codecs.getwriter('utf-8')(sys.stdout)

Beware of this approach! Some third-party libraries may depend on the default encoding being ascii and break. Note that this whole mess has been resolved in Python 3 (which defaults to UTF-8 encoding)

Saturday, August 21, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :