Date: Wed, 27 Jul 2011 08:45:30 +0100 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: freebsd-questions@freebsd.org Subject: Re: Book recommendations (slightly OT) Message-ID: <4E2FC21A.6070300@infracaninophile.co.uk> In-Reply-To: <4E2F1C0F.4040108@msen.com> References: <4E2F1C0F.4040108@msen.com>
next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig224968623DC22F57A41CB9A5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 26/07/2011 20:57, Mark Moellering wrote: > I want to automate some tasks, creating directories, file editing, etc.= =20 > 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 the= m. 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 --=20 Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate JID: matthew@infracaninophile.co.uk Kent, CT11 9PW --------------enig224968623DC22F57A41CB9A5 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.16 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk4vwiIACgkQ8Mjk52CukIx+yQCdFNQg7e5QtEWgInb+dwZot5mr zA4AoIwcAjJ86fqzl7vIF6t3s93W0DWW =xFIw -----END PGP SIGNATURE----- --------------enig224968623DC22F57A41CB9A5--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4E2FC21A.6070300>