[X-Unix] A little shell scripting help

Larry Helms lhelms at sonic.net
Tue Oct 4 08:56:57 PDT 2005


Another EXCELLENT resource for shell scripting newbies...

    http://www.shelldorado.com

Contains tutorials... And a WHOLE BUNCH of exmaples.


Now... I shall pipe in about the "if (...)" issue originally raised...

I shall EXCLUDE the csh or tcsh shell for the reasons mentioned in
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/


Depending on the shell the (...) construct usually means execute contents
INLINE.  E.g.

( ls -1; head -1 )

Each time Unix shells hit a command they really fork off another process to
perform the task - having used other OSes... I won't even go into that issue
- anyway... A way to force the routine NOT to do this... Is to enclose the
commands in ( ) sets.  So... As you can see... You shouldn't really be using
if ( $retry > 1 ).  Some shells use (( )) to perform arithmatic
expressions/evaluations.  If the shell you are using does support this
construct... You should use it... Or the more portable version using the [
$var -gt val ]

So... In essence what you are trying to do... Can be written using one of
These - depending on the shell:

if (( $retry > 1 ))
if [ $retry -gt 1 ]

It is best to do a 'man {shellname}' to check out what your shell can/should
be doing.

... And remember SPACES _ARE_ important

Something else to consider... Is portability. sh, ksh, zsh, etc are NOT ALL
THE SAME on ALL OSes.  Hardware vendors tend to throw in/change stuff.  It
is best to pick a shell that more widely available (such as sh) ... Code to
its standards... And then tweak your script as necessary to run on various
Oses - if need be.

If you are merely a hobbyist and aren't coding for portability... Choose the
shell that you are most comfortable using - get to know it... And then move
on to others - if you so desire.  Personally, on the Mac I prefer the zsh
shell.



More information about the X-Unix mailing list