我有一个交互式的FORTRAN程序,需要用户的各种输入。现在,我想将这个Fortran程序的输出存储到一个变量中,并在shell脚本中使用这个值。我试过了

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

但在这两种情况下,它都不会提示用户输入并保持空闲状态。我该怎么办?
一段fortran代码示例如下
  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

然后我用了这样的剧本
 test.sh

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

我相信有件小事我找不到。请帮帮我。
这只是一个示例代码和脚本,我的实际脚本和代码大不相同。

最佳答案

让·弗朗索瓦·法布是对的。

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

问题是标准差(0),答案是标准差(5),结果是标准差(6)
var=`./test`

在那之后效果很好。

10-07 13:00
查看更多