Home > hs-utils

hs-utils

Hs-utils is a project mainly written in HASKELL and SHELL, it's free.

Haskell through classic UNIX commands.

As long as GHC is installed, you can build the sample program with:

:; make cat

You can run it like this:

:; ./cat Makefile

The ./' is important. We wouldn't want to run the systemcat' instead!

The `cat.hs' program demoes some basic UNIX utility idioms, idioms that are good to follow for command line utilities. When the arguments are not expected, it exits with a non-zero status; when it reports errors, it uses STDERR and not STDOUT (this prevents mingling meaningful output with diagnostic messages).

Let's modify it be more like the real `cat'.

. Cause it to `cat' STDIN when there are no input files.

. If there's more than one input file, it should `cat' them in order.

. If one of the input arguments is -', then it should cat STDIN as well as any other input files. (Using-' as a standin for STDIN is common to many shell utilities.)

Please find commentary below.

                                                  STDIN, STDOUT and STDERR

A UNIX process generally starts with three open "file descriptors" or
"handles". These are STDIN (standard input), STDOUT (standard output) and
STDERR (standard error).

STDERR is better understood as "standard commentary". It's best to
separate remarks and warnings from real output -- it's essential for
machine processable output -- and so we have one channel for output and
one for commentary.

                                                               Exit Status

A simple way to examine the exit status is with `$?':

 :; ./cat Makefile cat
  Too many arguments.
 :; echo $?
  1

 :; ./cat Makefile
  < some output >
 :; echo $?
  0

So `0' means okay and anything else means something bad happened. Why is
it important to set the exit status correctly? The shell operators `&&'
and `||', as well as the if statement, really on the exit status to know
what to do next.

 :; ./cat Makefile && echo 'Success!'

We see how `&&' let's us run one command conditional on the success of
another. It's important to help out people who are scripting on top of our
tools by setting the exit status appropriately.
Previous:sample