[英]Piping output of a failed command in windows command prompt (cmd.exe)
[英]Using piping in Windows command line to run simple Fortran exe
我有一個 Fortran 可執行文件,它在進行計算之前需要幾個鍵盤輸入。 它在輸入輸入的終端中工作,但我想使用管道輸入輸入(Windows 10 命令行)。 當我只有一個鍵盤輸入時,管道工作得很好。 我怎樣才能讓它與多個輸入一起工作?
我幾乎可以根據這里的類似問題讓它工作: Passing arguments to interactive fortran program 。 我查看了有關標准輸入的文檔,但我仍然無法弄清楚。
這是一個只有一個輸入的示例 Fortran 程序,它與管道一起工作。
program foo
integer n
character*80 c
write (*,*) 'Enter 1,2,3 for cat,dog,fish'
read (*,*) n
if (n .eq. 1) then
write (*,*) 'meow'
elseif (n .eq. 2) then
write (*,*) 'woof'
elseif (n .eq. 3) then
write (*,*) 'blurp'
else
write (*,*) 'error1'
endif
C write (*,*) 'Enter y,n for yay,nay'
C read (*,*) c
C if (c == 'y') then
C write (*,*) 'yes'
C elseif (c == 'n') then
C write (*,*) 'no'
C elseif (n .eq. 3) then
C else
C write (*,*) 'error2'
C endif
end
終端測試:
C:\my\file\path> C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
2
woof
C:\my\file\path> echo 1 | C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
meow
這是一個帶有多個輸入的示例 Fortran 程序,它不適用於管道。
與上面相同的程序,但注釋行未注釋。 終端測試:
C:\my\file\path> C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
3
blurp
Enter y,n for yay,nay
n
no
C:\my\file\path> echo 1 y | C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
meow
Enter y,n for yay,nay
At line 18 of file C:/my/file/path/foo.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file
Error termination. Backtrace:
Could not print backtrace: libbacktrace could not find executable to open
#0 0x318dd91b
#1 0x318d6b34
#2 0x318d355b
#3 0x318d7f6c
#4 0x318e8e9d
#5 0x318d88df
#6 0x318d5190
#7 0x318b1691
#8 0x318f3f93
#9 0x318b13c0
#10 0x318b14f5
#11 0xb9677c23
#12 0xba24d4d0
#13 0xffffffff
編譯器信息: GNU gfortran, CMake based off wiki example
您有兩個簡單的讀取語句。 每個讀取一條記錄 = 一行文本。 您需要提供兩條記錄或讓程序在舊記錄處繼續。
在bash 中,我可以執行echo -e "2 \ny" |./a.out
將兩條記錄放入一個echo
命令中,但我不確定 Windows cmd。在 Powershell 中,您應該使用Echo 換行符到 powershell 控制台。 這意味着
echo "2 `n y"
如果您可以更改您的 Fortran 代碼,請使用非高級輸入:
read (*,'(i1)',advance="no") n
有了這個改變,我可以做到
> echo -e "2 y" | ./a.out
Enter 1,2,3 for cat,dog,fish
woof
在 bash 中測試,但應該在其他地方工作,包括 cmd。
對於 windows cmd,你可以使用 for 循環。
(for %i in (2 y) do @echo %i)|C:\my\file\path\foo.exe
Enter 1,2,3 for cat,dog,fish
woof
Enter y,n for yay,nay
yes
如果您將其放入 a.bat 或 .cmd,請記住使用兩個 % 符號。 有很多方法可以做到這一點。 請參閱如何在批處理文件中回顯換行符?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.