2

I have a fortran program (which I cannot modify) that requires several inputs from the user (in the command line) when it is run. The program takes quite a while to run, and I would like to retain use of the terminal by running it in the background; however, this is not possible due to its interactive nature.

Is there a way, using a bash script or some other method, that I can pass arguments to the program without directly interacting with it via the command line?

I'm not sure if this is possible; I tried searching for it but came up empty, though I'm not exactly sure what to search for.

Thank you!

ps. I am working on a unix system where I cannot install things not already present.

user3470516
  • 197
  • 1
  • 2
  • 7

1 Answers1

5

You can pipe it in:

$ cat delme.f90
program delme
    read(*, *) i, j, k
    write(*, *) i, j, k
end program delme

$ echo "1 2 3" | ./delme
           1           2           3

$ echo "45 46 47" > delme.input
$ ./delme < delme.input
          45          46          47

$ ./delme << EOF
> 3 2 1
> EOF
           3           2           1
chw21
  • 7,970
  • 1
  • 16
  • 31
  • Thank you! This is exactly what I needed, and I had no idea that it was even possible, let alone so simple. I particularly like the second solution. – user3470516 May 19 '15 at 04:20
  • Is this answer specific to `fortran 90`? Or perhaps a specific fortran compiler (`gfortran` vs `f77`). I have the same question, but am working with some legacy fortran 77 code. – RTbecard Jan 31 '19 at 14:13
  • This is independent of the compiler, it works with the shell. So it would depend on your operating system. Any Linux or MacOS should be fine, but I don't know about Windows. – chw21 Feb 01 '19 at 02:33
  • 1
    You can also use here-strings: `./program <<< 42`. If `program` reads input more than once, you can insert newlines using ANSI C strings `./program <<< $'42\n19'`. – Arch Stanton Apr 03 '19 at 07:11