hscrtmpl (empty) → 1.0
raw patch · 4 files changed
+191/−0 lines, 4 filesdep +basedep +directorydep +old-localesetup-changed
Dependencies added: base, directory, old-locale, old-time
Files
- LICENSE +31/−0
- Setup.hs +2/−0
- hscrtmpl.cabal +25/−0
- hscrtmpl.hs +133/−0
+ LICENSE view
@@ -0,0 +1,31 @@+Copyright Dino Morelli 2013++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Dino Morelli nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hscrtmpl.cabal view
@@ -0,0 +1,25 @@+name: hscrtmpl+version: 1.0+cabal-version: >= 1.8+build-type: Simple+license: BSD3+license-file: LICENSE+copyright: 2013 Dino Morelli+author: Dino Morelli+maintainer: Dino Morelli <dino@ui3.info>+stability: experimental+homepage: http://ui3.info/darcs/hscrtmpl/+synopsis: Haskell shell script template+description: A template for writing shell scripts in Haskell. Contains some useful functions and examples of things commonly done in bash.+category: Application, Console, Scripting+tested-with: GHC >= 7.6.2++source-repository head+ type: darcs+ location: http://ui3.info/darcs/hscrtmpl++-- Hey! hscrtmpl.hs is a *script*, you don't have to build it unless+-- you want to have a binary!+executable hscrtmpl+ main-is: hscrtmpl.hs+ build-depends: base >= 3 && < 5, directory, old-locale, old-time
+ hscrtmpl.hs view
@@ -0,0 +1,133 @@+#! /usr/bin/env runhaskell++{- This is a 'skeleton file' for writing shell scripts with Haskell++ Haskell is my go-to language for anything more complicated than a+ simple sequence of shell commands in a bash script. Doing list+ processing and a lot of logic in bash is a grind, to me.++ The idea here is to take a copy of this script and hack it to do+ what you need quickly. Throw the rest out.++ The script starts off (after the imports) with some simple examples+ in main.++ After that are a few functions that simplify things like getting+ the date as a String, logging a date-stamped String to stdout and+ manupulating an ExitCode as a true/false value.++ Finally, there is a comment block full of examples in bash and some+ Haskell code to do something along the same lines.++ Dino Morelli <dino@ui3.info>+ http://ui3.info/d/proj/hscrtmpl.html+ version: 1.0+-}++--import Control.Monad ( when, unless )+--import System.Cmd+import System.Directory+--import System.Environment+import System.Exit+import System.Locale+--import System.Process+import System.Time+import Text.Printf+--import Text.Regex+++main :: IO ()+main = do+ printf "This is a shell script\n"++ putStrLn =<< date++ home <- getHomeDirectory+ let prompt = printf "HOME is %s" home+ putStrLn prompt++ logM "Example of a log message"+++{- Get the current date/time as a string in RFC822 format+ Looks like this in my locale: Mon Feb 13 16:21:38 EST 2012+-}+date :: IO String+date = dateFormat "%c"+++{- Get the current date/time as a string in the specified format+ For format string help, see man 3 strftime+-}+dateFormat :: String -> IO String+dateFormat fmt = fmap (formatCalendarTime defaultTimeLocale+ fmt) (getClockTime >>= toCalendarTime)+++{- Output a message with datestamp+-}+logM :: String -> IO ()+logM msg = do+ tstamp <- dateFormat "%Y-%m-%d %H:%M:%S"+ printf "%s> %s\n" tstamp msg+++{- Turn an exit code (say, from system) into a Bool+-}+ok :: ExitCode -> Bool+ok ExitSuccess = True+ok _ = False+++{- Some common bash things and their Haskell counterparts:++ bash Haskell+ ---- -------+ dates System.Locale, System.Time+ date date++ date +"%Y%m%d" dateFormat "%Y%m%d"+ result: 20120213+ (these two functions above)++ file/dir things System.Directory+ $HOME getHomeDirectory+ [ -f FILE ] doesFileExist FILE+ [ -d DIR ] doesDirectoryExist DIR+ pwd getCurrentDirectory+ cd DIR setCurrentDirectory DIR++ environment variables System.Environment+ $VAR getEnv "VAR"++ arguments System.Environment+ arg1=$1 (arg1 : arg2 : _) <- getArgs+ arg2=$2++ string interpolation Text.Printf+ "foo $bar ${baz}" printf "foo %s %d" bar baz++ execution, exit code System.Cmd+ program -x val arg ec <- system "program -x val arg"+ ec=$?++ execution, capture stdout System.Process+ output=$(program -x val arg) output <- readProcess "program"+ ["-x", "val", "arg"]+ "stdin data, if desired"++ or use System.Process.readProcessWithExitCode+ :: FilePath -> [String] -> String -> IO (ExitCode, String, String)+ program args stdin exitcode stdout stderr++ exiting System.Exit+ exit 0 exitSuccess+ exit 1 exitFailure+ exit INT exitWith $ ExitFailure INT++ regular expressions Text.Regex+ (see bash docs for =~ mbMatches = matchRegex+ and BASH_REMATCH) (mkRegex "a(.)b(.)") string+ :: Maybe [String]++-}