[sf-lug] Learning Bourne Scripting

Mike Higashi mhigashi at gmail.com
Fri Jun 24 17:55:08 PDT 2011

On Thu, Jun 23, 2011 at 3:31 PM, Rick Moen <rick at linuxmafia.com> wrote:
> Maybe there's not a single bash tutorial that's best for everyone.

bash is pretty powerful.  There were times in the past when I was
asked if something could be done with bash, and I said no, only to
find out much later that I was wrong.

You don't really know if bash is capable of something until you get
an itch to scratch, and really look deeply into whether bash can cure
that itch.  For a tutorial, everybody who has a specific interest will
want to learn different things, so once you get past the bare beginnings
their needs will diverge.

There are some things that ought to be taught to all beginners, in
the area of good scripting practices, sanity checks, and like.  Does
the script test to see if a directory exists before it tries to copy a file
into it?  Did the copy succeed, or fail?  To err is human, but to really
screw things up takes a computer (or this case, a poorly written script
that blindly executes without testing if it's doing the right thing.)

BTW, some stupid bash tricks that I've done:

  * mass rename of a large number of improperly named files,
  for example from foo1.bar to foo1.baz (this can be done in a
  single command, not a loop, if you know how to use bash's
  substring syntax)

  * when copying a large number of files into a directory, read the last
  character of the filename and copy into a subdirectory based on
  that letter (to prevent filesystem slowdown from too many entries
  in one directory)

  * the same as above, but go two letters deep instead of
  just one

  * perform a pseudo-random copy of graphics files into some
  new user account webpages

...and more stuff like that.


More information about the sf-lug mailing list