4

I have an interactive FORTRAN program that requires various inputs from the user. Now, I want to store the output of this Fortran program into a variable and use this value in a shell script. I tried

 var=`./test` and var=$(./test)

but in both the cases, it does not prompt the user for input and stays idle. What should I do? A piece of example fortran code is like this

  test.f  

  program test
  character*1 resp1, resp3
  integer resp2, ans

  write(*,*) 'Think of a prime number less than 10'
  write(*,*) 'Say whether it is odd or even'
  write(*,*) 'Write o/e'
  read(*,*) resp1
  if ( resp1 .EQ. 'e' ) then
   ans=2
  else
   write(*,*) 'Is the number close to 4 or 8'
   read (*,*) resp2
   if ( resp2 == 8 ) then
    ans=7
   else
    write(*,*) 'Is the number greater than or less than 4'
    write(*,*) 'Write g or l'
    read (*,*) resp3
    if ( resp3 .EQ. 'l' ) then
     ans=3
    else
     ans=5
    end if
   end if
  end if
  write(*,*) ans
  end

  Compiled as gfortran test.f -o test

Then I used a script like this

 test.sh

 var=`./test`
 echo "The answer you are looking for is " $var

I believe there is something very trivial that I am not able to find. Please help me.

P.S. This is just an example code and script and my actual script and code is far different.

1 Answers1

3

Jean-François Fabre is right.

program test 
character*1 resp1, resp3 
integer resp2, ans 

write(0,*) 'Think of a prime number less than 10' 
write(0,*) 'Say whether it is odd or even' 
write(0,*) 'Write o/e' 
read(5,*) resp1 
if ( resp1 .EQ. 'e' ) then 
 ans=2 
else 
 write(0,*) 'Is the number close to 4 or 8' 
 read (5,*) resp2 
 if ( resp2 == 8 ) then 
  ans=7 
 else 
  write(0,*) 'Is the number greater than or less than 4' 
  write(0,*) 'Write g or l' 
  read (5,*) resp3 
  if ( resp3 .EQ. 'l' ) then 
   ans=3 
  else 
   ans=5 
  end if 
 end if 
end if 
write(6,*) ans 
end 

Questions are stderr (0), Answers are stdin (5), Result is stdout (6)

var=`./test`

works fine after that.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 0, 5 and 6? Just out of curiosity, what are 1, 2, 3 and 4? – James Brown Oct 25 '16 at 12:10
  • 1
    Thanks so much. It works. I always used write(*,*). I realized today the importance of the numbers within. – Himangshu Paul Oct 25 '16 at 12:16
  • 1
    @JamesBrown : As far as I know, they can be defined to be files. 0,5 and 6 is just an old convention, pretty much like everything else in Fortran :D https://en.wikipedia.org/wiki/Standard_streams – Eric Duminil Oct 25 '16 at 12:17
  • 1
    Probably a card deck reader, and a chisel and tablet... And not a Samsung tablet, but a Flintstone's tablet. – Holmz Oct 25 '16 at 20:57
  • 3
    Use of the ERROR_UNIT, OUTPUT_UNIT and INPUT_UNIT named constants from the ISO_FORTRAN_ENV intrinsic module, rather than the integer literal values, will be more robust and portable, though there is no requirement that output to ERROR_UNIT be distinct from OUTPUT_UNIT. – IanH Oct 25 '16 at 21:40