簡體   English   中英

haskell和Unix shell腳本

[英]haskell and Unix shell scripting

是否有可能在unix shell腳本中使用haskell函數?

例如:

#!/bin/bash

...
var1=value
...

# use haskell function with input from shell variable var1
# and store the result into another shell variable var2

var2=haskellFunction $var1

...

我想在shell腳本中使用變量作為haskell函數的參數和結果

提前致謝。

吉米

使用-e開關到ghc ,例如

var2=$(ghc -e "let f x = x*x + x/2 in f $var1")

對於字符串處理,最好將Haskell的interact與bash的字符串結合使用:

var2=$(ghc -e 'interact reverse' <<<$var1)

此外,您可以使用ghci編寫腳本:只需將shebang #!/usr/bin/env runhaskell放在腳本的第一行並使其可執行。 這些腳本可以像往常一樣從sh/bash/ksh/csh/whatever腳本調用。

例如

$ cat > hello.hsh
#!/usr/bin/env runhaskell

main = putStrLn "Hello world!"

$ chmod +x hello.hsh
$ ./hello.hsh
Hello world!
$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM