10

is there any possibility to use haskell functions in unix shell scripts?

for instance:

#!/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

...

I want to use variables in shell scripts as arguments and results of haskell functions

Thanks in advance.

jimmy

jimmyt
  • 491
  • 4
  • 10

2 Answers2

10

Use the -e switch to ghc, e.g.

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

For string processing, it is best to use Haskell's interact in conjunction with bash's here strings:

var2=$(ghc -e 'interact reverse' <<<$var1)
dave4420
  • 46,404
  • 6
  • 118
  • 152
2

Also, you can write scripts using ghci: Just put shebang #!/usr/bin/env runhaskell at the first line of the script and make it executable. Such scripts may be called from sh/bash/ksh/csh/whatever scripts as usual.

E.g.

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

main = putStrLn "Hello world!"

$ chmod +x hello.hsh
$ ./hello.hsh
Hello world!
$
Dmytro Sirenko
  • 5,003
  • 21
  • 26
  • Can anybody explain the significance of `/usr/bin/env`? I'm not familiar with that particular command... – MathematicalOrchid Nov 05 '12 at 22:01
  • @MathematicalOrchid I think it's needed to make script independent from the `runhaskell` absolute path (it may be in /usr/local/bin, /opt/local/bin and other weird places, it is not always good to assume it to be in /usr/bin). – Dmytro Sirenko Nov 06 '12 at 16:38
  • @MathematicalOrchid quoting http://en.wikipedia.org/wiki/Env : "Note that it is possible to specify the interpreter without using env, by giving the full path of the python interpreter. A problem with that approach is that on different computer systems, the exact path may be different. By instead using env as in the example, the interpreter is searched for and located at the time the script is run" – Dmytro Sirenko Nov 06 '12 at 16:40