Safe Haskell | None |
---|---|
Language | Haskell98 |
A shell script monad
- data Script a
- script :: Script f -> Text
- linearScript :: Script f -> Text
- data Var
- val :: Var -> Quoted Text
- data Quoted a
- quote :: Text -> Quoted Text
- run :: Text -> [Text] -> Script ()
- cmd :: ShellCmd result => Text -> result
- class CmdArg a
- newtype Output = Output (Script ())
- comment :: Text -> Script ()
- newVar :: Text -> Script Var
- newVarContaining :: Text -> Text -> Script Var
- globalVar :: Text -> Script Var
- func :: Script () -> Script (Script ())
- (-|-) :: Script () -> Script () -> Script ()
- forCmd :: Script () -> (Var -> Script ()) -> Script ()
- whileCmd :: Script () -> Script () -> Script ()
- ifCmd :: Script () -> Script () -> Script () -> Script ()
- whenCmd :: Script () -> Script () -> Script ()
- unlessCmd :: Script () -> Script () -> Script ()
- readVar :: Var -> Script ()
- stopOnFailure :: Bool -> Script ()
- ignoreFailure :: Script () -> Script ()
Documentation
script :: Script f -> Text Source
Generates a shell script, including hashbang, suitable to be written to a file.
linearScript :: Script f -> Text Source
Generates a single line of shell code.
A shell variable.
A value that is safely quoted.
quote :: Text -> Quoted Text Source
Quotes the value to allow it to be safely exposed to the shell.
The method used is to replace ' with '"'"' and wrap the value inside single quotes. This works for POSIX shells, as well as other shells like csh.
cmd :: ShellCmd result => Text -> result Source
Variadic argument version of run
.
The command can be passed any number of CmdArgs.
Convenient usage of cmd
requires the following:
{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-} {-# OPTIONS_GHC -fno-warn-type-defaults #-} import Control.Monad.Shell import qualified Data.Text.Lazy as L default (L.Text)
This allows writing, for example:
demo = script $ do cmd "echo" "hello, world" name <- newVar "name" readVar name cmd "echo" "hello" name
toTextArg
The output of a command, or even a more complicated Script
can be passed as a parameter to cmd
Examples:
cmd "echo" "hello there," (Output (cmd "whoami")) cmd "echo" "root's pwent" (Output (cmd "cat" "/etc/passwd" -|- cmd "grep" "root"))
Defines a new shell variable.
The name of the variable that appears in the shell script will be based on provided name (which can be mempty), but each call to newVar will generate a new, unique variable name.
Creates a new shell variable, with an initial value.
func :: Script () -> Script (Script ()) Source
Defines a shell function, and returns an action that can be run to call the function.
TODO parameter passing to the function
forCmd :: Script () -> (Var -> Script ()) -> Script () Source
Runs the command, and separates its output into parts (using the IFS)
The action is run for each part, passed a Var containing the part.
whileCmd :: Script () -> Script () -> Script () Source
As long as the first Script exits nonzero, runs the second script.
ifCmd :: Script () -> Script () -> Script () -> Script () Source
if with a monadic conditional
If the conditional exits 0, the first action is run, else the second.
readVar :: Var -> Script () Source
Generates shell code to fill a variable with a line read from stdin.
stopOnFailure :: Bool -> Script () Source
By default, shell scripts continue running past commands that exit nonzero. Use "stopOnFailure True" to make the script stop on the first such command.
ignoreFailure :: Script () -> Script () Source
Makes a nonzero exit status be ignored.