Book recommendations (slightly OT)

Matthew Seaman m.seaman at infracaninophile.co.uk
Wed Jul 27 07:45:44 UTC 2011


On 26/07/2011 20:57, Mark Moellering wrote:
> I want to automate some tasks, creating directories, file editing, etc. 
> I was going to pick up a book on shell scripting but wanted to ask the
> list if;
> A) I am barking up the wrong tree and should use something else.

No -- automating routine tasks is exactly what shell scripting is for.

> B) If I am headed in the right direction, what is the best book /
> resource to use?

Personally, I wouldn't spend any money on textbooks trying to teach you
shell programming.  Not because there aren't any good books available,
but because the free on-line resources are more than adequate to get you
going.

First of all, choose your shell.  On FreeBSD I'd say that it's got to be
/bin/sh for programming.  This is the POSIX compatible Bourne Shell.  If
you write your scripts to the POSIX standard then you'll be able to run
them just about anywhere eg. using bash on a Linux box.  The converse is
not true.

You could learn bash -- it is pretty much a de-facto standard nowadays
-- but bash is pretty bloated with lots of interactive usage stuff, and
there's nothing you can't do in POSIX shell that you can in bash.  Also,
bash has to be installed from ports, which might not seem like a big
deal (usually it isn't), but it tends to become really quite important
when you're dealing with systems in extremis.

Don't bother trying to use tcsh for programming -- that's not what it is
for.  tcsh is great interactively (it's what I use for my login shell),
but a pain in the bum for scripting.

Now, resources for learning how to program in /bin/sh --

 * The sh(1) man page is invaluable.  It's a really nicely written and
   concise description of what sh can do.  I'm constantly referring to
   this man page when shell scripting.

 * Code examples.  Copying from what someone else wrote really is the
   best way to get ahead.  There are many good examples that come with
   FreeBSD -- look at the periodic scripts, rc scripts (including from
   ports) and things like mergemaster(1).  For instance, if you want to
   see how to deal with command line arguments, the standard idiom is
   very clearly demonstrated in mergemaster.

 * On-line resources like http://tldp.org/LDP/abs/html/ (Yes -- this is
   all about bash, but there's a lot of overlap with sh)

 * Learn about all of those Unixy commands.  /bin/sh in many ways is
   designed as a means to glue together compiled C programs to achieve a
   desired effect.  You should be familiar with programs like test(1),
   jot(1), comm(1), xargs(1), printf(1), comm(1), sort(1).  Not to
   mention those stalwarts of shell programming sed(1) and awk(1) --
   although each of those is in itself is a programming language about
   as complex as pure shell.

Counterintuitively, given the above, the best shell scripts use built-in
shell capabilities rather than calling out to external programmes
wherever possible.  eg. Using the variable prefix / suffix selection
operators: ${progname%%*/} has much the same effect as basename(1).

All the usual programming best-practices apply in shell scripting: write
clean, well structured code divided into relatively short functions each
of which has a single specific purpose.  Avoid overuse of global
variables and magic side-effects.  Prefer clarity over cleverness.
Comment liberally, but make sure your comments add value.  Choose
conventions (eg. on variable naming and code formatting) and stick to them.

One other piece of advice -- as a matter of style, try and avoid
interactive behaviour in scripts.  If you prompt a user to type in some
value, then it makes it very hard to call your script from another
script.  Instead, pass in any values you need using the command line, or
by using environment variables.

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
JID: matthew at infracaninophile.co.uk               Kent, CT11 9PW

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 267 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20110727/c5442be2/signature.pgp


More information about the freebsd-questions mailing list