Write shell scripts with Conduit. Still in the experimental phase.
import Control.Monad.IO.Class
import Data.Conduit.Shell
import System.Directory
main =
run (do exists <- liftIO (doesDirectoryExist "fpco")
if exists
then rm "fpco/.hsenvs" "-rf"
else git "clone" "git@github.com:fpco/fpco.git"
liftIO (setCurrentDirectory "fpco")
shell "./dev-scripts/update-repo.sh"
shell "./dev-scripts/build-all.sh"
alertDone)
Piping of processes and normal conduits is possible:
λ> run (ls $= grep "Key" $= shell "cat" $= CL.map (second (S8.map toUpper)))
KEYBOARD.HI
KEYBOARD.HS
KEYBOARD.O
Results are outputted to stdout unless piped into other processes:
λ> run (do shell "echo sup"; shell "echo hi")
sup
hi
λ> run (do shell "echo sup"; sed "s/u/a/"; shell "echo hi")
sup
hi
λ> run (do shell "echo sup" $= sed "s/u/a/"; shell "echo hi")
sap
hi
Live streaming between pipes like in normal shell scripting is possible:
λ> run (do tail' "/tmp/example.txt" "-f" $= grep "--line-buffered" "Hello")
Hello, world!
Oh, hello!
(Remember that grep
needs --line-buffered
if it is to output things
line-by-line).
Process errors can be ignored by using the Alternative instance.
import Control.Applicative
import Control.Monad.Fix
import Data.Conduit.Shell
main =
run (do ls
echo "Restarting server ... ?"
killall name "-q" <|> return ()
fix (\loop ->
do echo "Waiting for it to terminate ..."
sleep "1"
(ps "-C" name $= discardChunks >> loop) <|> return ())
shell "dist/build/ircbrowse/ircbrowse ircbrowse.conf")
where name = "ircbrowse"
import Data.Conduit.Shell
main =
run (do xmodmap ".xmodmap"
xset "r" "rate" "150" "50")
All executable names in the PATH
at compile-time are brought into
scope as runnable process conduits e.g. ls
or grep
.
Stdin/out and stderr are handled as an Either type.
type Chunk = Either ByteString ByteString
Left
is stderr, Right
is stdin/stdout.
All processes are bound as variadic process calling functions, like this:
rmdir :: ProcessType r => r
ls :: ProcessType r => r
But ultimately the types end up being:
rmdir "foo" :: Conduit Chunk m Chunk
ls :: Conduit Chunk m Chunk
ls "." :: Conduit Chunk m Chunk
Etc.
Run all shell scripts with
run :: (MonadIO m, MonadBaseControl IO m)
=> Conduit Chunk (ShellT m) Chunk -> m ()
The ShellT
type has a handy Alternative
instance and can store
info like whether to echo all processes run similar to Bash's set -x
.
If using OverloadedStrings
so that you can use Text
for arguments,
then also enable ExtendedDefaultRules
, otherwise you'll get
ambiguous type errors.
{-# LANGUAGE ExtendedDefaultRules #-}
But this isn't necessary if you don't need to use Text
yet. Strings
literals will be interpreted as String
. Though you can pass a value
of type Text
or any instance of CmdArg
without needing conversions.
You might want to import the regular Conduit modules qualified, too:
import qualified Data.Conduit.List as CL
Which contains handy functions for working on streams in a list-like way. See the rest of the handy modules for Conduit in conduit-extra.
Also of interest is csv-conduit, html-conduit, and http-conduit.
Finally, see the Conduit category on Hackage for other useful libraries: http://hackage.haskell.org/packages/#cat:Conduit
All of these general purpose Conduits can be used in shell scripting.