I have a file named "test.txt"
with this text:
Good
Morning
Sir
and a file named "test.hs"
with the following code:
module Main where
import System.IO
main :: IO ()
main = interact f
f :: String -> String
f s = head $ lines s
The following command...
cat test.txt | runhaskell test.hs
outputs
Good
However, I would like to explicitly pass parameters to runhaskell
without relying on a file, like:
echo "Good\nMorning\nSir" | runhaskell test.hs
and also execute runhaskell
with a literal string of Haskell code, like:
echo "Good\nMorning\nSir" | runhaskell "module Main where\nimport System.IO\nmain :: IO ()\nmain = interact f\nf :: String -> String\nf s = head $ lines s"
Is this technically possible ?
CodePudding user response:
The problem is that echo will output a backslash (\
) and an n
instead of a new line.
You can use the -e
flag [unix.com], this flag will:
-e
enable interpretation of backslash escapes
So we can pass a string with new lines to runhaskell
's input channel with:
echo -e -- "Good\nMorning\nSir" | runhaskell test.hs
Note that cat test.txt | runhaskell test.hs
is useless use of cat
, you can replace this with:
runhaskell test.hs < test.txt
which is more efficient since we do not use a cat
process or the pipe to pass data to.