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

3
  • My spider-sense tells me I read about somebody writing a package for easily scripting shell-like stuff from Haskell... but I can't remember any details about it. :-( Commented Nov 4, 2012 at 20:25
  • @MathematicalOrchid Shelly? Commented Nov 4, 2012 at 20:41
  • @dave4420 Yeah, that may well be it... Commented Nov 4, 2012 at 20:44

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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!
$

3 Comments

Can anybody explain the significance of /usr/bin/env? I'm not familiar with that particular command...
@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).
@MathematicalOrchid quoting 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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.