1

I have a shell script file named /path/to/shell_script.sh which contains a function defintion shell_function() { ...}.

I'd like to be able to do something in Haskell like readProcessWithExitCode shell_function [] "" to eventually get a hold of an IO (String).

How to do this?

1
  • This question is very broad. Please give some actual code and a description of the problem Commented Oct 28, 2016 at 19:01

1 Answer 1

3

If you have a shell script like

#!/bin/bash

foo() {
    echo "foo"
}

you can use the readCreateProcess function from the process package to source the script and execute the function in one go, like this:

module Main where

import System.Process

main :: IO ()
main = do
    -- I had to put the full path of the script for it to work
    result <- readCreateProcess ((shell ". /tmp/foo.sh && foo")) ""
    print result

This solution assumes that the script only does things like defining functions and setting environment variables, without running undesired "effectful" code each time it is sourced.

Sign up to request clarification or add additional context in comments.

Comments

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.