How to invoke bash or shell scripts from a haskell program?

前端 未结 4 2297
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 22:16

I\'m writing some shell scripts with haskell, which I\'m running in gitbash, but there are a few other existing scripts I\'d like to be able to use from those scripts.

相关标签:
4条回答
  • 2020-12-29 22:44

    You can use System.Process. For example, executing seq 1 10 shell command:

    > import System.Process
    
    > readProcess "seq" ["1", "10"] ""
    "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
    it :: String
    
    > readProcessWithExitCode  "seq" ["1", "10"] ""
    (ExitSuccess,"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n","")
    it :: (GHC.IO.Exception.ExitCode, String, String)
    
    0 讨论(0)
  • 2020-12-29 22:52

    Of course. You can start by using system to invoke external processes.

    More sophisticated piping and process control is available in a cross-platform way from the System.Process library.

    Finally, you can consider porting your shell scripts to Haskell, via shell DSLs.

    0 讨论(0)
  • 2020-12-29 23:05

    Yes, it is possible. You can use process package, which exports many useful functions. Simplest one is System.Cmd.system, which can run some application in shell, yielding exit code.

    More advanced features are provided too in the System.Process module. With this module you can run process and communicate with it in many ways (input piping, exit codes, waiting for process to stop, modify its environment etc).

    0 讨论(0)
  • 2020-12-29 23:07

    Turtle is pretty nice modern haskell library for this.

    0 讨论(0)
提交回复
热议问题