我有一個"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使用 Haskell 代碼的文字字串執行,例如:
echo "Good\nMorning\nSir" | runhaskell "module Main where\nimport System.IO\nmain :: IO ()\nmain = interact f\nf :: String -> String\nf s = head $ lines s"
這在技術上可行嗎?
uj5u.com熱心網友回復:
問題是 echo 將輸出一個反斜杠 ( \) 和一個n而不是一個新行。
您可以使用-e標志 [unix.com],該標志將:
-e啟用反斜杠轉義的解釋
所以我們可以將帶有新行的字串傳遞給runhaskell's 的輸入通道:
echo -e -- "Good\nMorning\nSir" | runhaskell test.hs
請注意,使用cat test.txt | runhaskell test.hs是無用的cat,您可以將其替換為:
runhaskell test.hs < test.txt
這更有效,因為我們不使用cat行程或管道來傳遞資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405830.html
標籤:
