我有一個交互式FORTRAN程序,需要來自用戶的各種輸入。現在,我想將此Fortran程序的輸出存儲到變量中,並在shell腳本中使用此值。我試過將Fortran程序輸出到變量中
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
我相信有一些很瑣碎,我無法找到。請幫幫我。
P.S.這只是一個示例代碼和腳本,而我的實際腳本和代碼則完全不同。
提示輸出和變量輸出合併。不知道這是否可能,但你能提示用戶標準錯誤而不是標準輸出嗎?這將工作。 –
讓我試試。你的意思是使用$? ? –
我強烈建議您編寫自由格式的Fortran。請參閱:http://www.fortran90.org/src/best-practices.html – jlokimlin