我有一个名为"test.txt"的文件,其文本如下:
Good
Morning
Sir和一个名为"test.hs"的文件,其代码如下:
module Main where
import System.IO
main :: IO ()
main = interact f
f :: String -> String
f s = head $ lines s以下命令..。
cat test.txt | runhaskell test.hs
输出
Good
但是,我希望在不依赖文件的情况下显式地将参数传递给runhaskell,例如:
echo "Good\nMorning\nSir" | runhaskell test.hs
并使用哈斯克尔代码的文字字符串执行runhaskell,如:
echo "Good\nMorning\nSir" | runhaskell "module Main where\nimport System.IO\nmain :: IO ()\nmain = interact f\nf :: String -> String\nf s = head $ lines s"
这在技术上是可能的吗?
发布于 2022-01-08 15:54:48
问题是回声将输出一个反斜杠(\)和一个n,而不是一个新行。
您可以使用 flag [unix.com],此标志将:
-e支持反斜杠转义的解释
因此,我们可以通过以下方式将带有新行的字符串传递到runhaskell的输入通道:
echo -e -- "Good\nMorning\nSir" | runhaskell test.hs注意,cat test.txt | runhaskell test.hs是 ,可以将其替换为:
runhaskell test.hs < test.txt这更有效,因为我们不使用cat进程或传递数据的管道。
https://stackoverflow.com/questions/70633982
复制相似问题