Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

For example, I'd like to run maven goals or do a git pull, but without having to integrate specifically with those tools.

Is there a way to do this?

like image 378
Peter Hall Avatar asked May 10 '12 14:05

Peter Hall


People also ask

Are bash and shell scripts the same?

Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems. sh provides some built-in commands. bash is a superset of sh.

Is shell program and shell script is same?

In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands.


2 Answers

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) 
like image 87
ДМИТРИЙ МАЛИКОВ Avatar answered Sep 22 '22 09:09

ДМИТРИЙ МАЛИКОВ


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).

like image 33
Vladimir Matveev Avatar answered Sep 20 '22 09:09

Vladimir Matveev