Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Jul 2011 01:54:15 +0200
From:      Jerome Herman <jherman@dichotomia.fr>
To:        freebsd-questions@freebsd.org
Subject:   Re: Book recommendations (slightly OT)
Message-ID:  <4E2F53A7.3040906@dichotomia.fr>
In-Reply-To: <4E2F1C0F.4040108@msen.com>
References:  <4E2F1C0F.4040108@msen.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 26/07/2011 21: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.
> B) If I am headed in the right direction, what is the best book / 
> resource to use?
>
> Thanks in advance
>
> Mark Moellering
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to 
> "freebsd-questions-unsubscribe@freebsd.org"

The first thing to do is to define what you want to do with scripting.

Most users have now turned to bash, very easy and quite powerful, though 
it has some specifics you won't find in any other shell. Such as 
replacing certain simple commands on the shell line by its own internal 
version, which can be very frustrating. This said it is probably the 
easiest shell to learn given there are lots and lots of examples, 
tutorial and users around here.

For pure Unix/BSD/Solaris... professional administration, you have to 
learn tcsh/csh (basically the same thing, tcsh being an improved 
version).  Basically it is a bit like vi. Even if you do not like vi, 
but want to professionally maintain Unix/BSD/Solaris..., you have to 
learn it, because one day you will have to log on an old server and vi 
will be the only "modern" editor available. Csh/Tcsh will basically be 
installed on pretty much every computer you might find. And csh can be 
tricky at time if you only know Bash.

On the other hand if you are a user/dev just wanting to automate some of 
his daily routine, then you can go for pretty much any shell you want. I 
personally prefer zsh.

One shell that is great but you need to be aware of is ksh. The problem 
of ksh is that it is so different from every other shell that learning 
it is a bit of a trouble. It is hard to find good example, and it is 
hard to transcribe ksh scripts and logic unto an other shell.

I can only advise you to browse around, look at what every shell has to 
offer and pick one. Do not hesitate to change if you are not happy.

As far as learning a shell goes, well it is more about going for net 
tutorials and reading man pages over and over again. At first you will 
be using "cat", "|" and ">" a lot. That is normal, but the only way to 
progress is to try to use them all as little as possible. (Which 
generally translates into reading the man page again).

Last thing, though it is considered to be a "welcome ritual" among 
admins, do backups, lots of backups, and test your scripts with another 
account that cannot destroy all your files at once. When learning to 
script you will one day make a stupid mistake, it will be a very simple 
script and a very stupid mistake. But you will be very happy you have a 
backup when the worst happens.

Classical mistakes involves making a find with exec, but forgetting to 
target real files only (such as removing all 0 bytes files from a system 
=> say goodbye to /dev, links, sockets etc.) and running a script with a 
badly set var (like export deluser="FOO"; rm -rf "/hom/$delusr").


Good luck on your learning.
Jerome Herman



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4E2F53A7.3040906>