Finding the last arg

Chuck.Phillips Chuck.Phillips at FtCollins.NCR.COM
Tue Jan 15 02:43:04 AEST 1991


>>>>> On 5 Jan 91 15:56:45 GMT, jc at minya.UUCP (John Chambers) said:
John> With the above for-loop, this gives
John> 	for last;do :;done
John> which doesn't work.  The shell gives a syntax error, complaining
John> about an unexpected ';' in the line.  Myself, I found this to be a
John> somewhat unexpected error message.  It appears my simple-minded
John> algorithm for condensing code doesn't work in this case.

John> So what's going on here?  What the @#$^&#( is the shell's syntax that 
John> makes the semicolon not only unneeded, but illegal in this case?

A little sh history: sh comments originally went from a ':' to the end of a
line.  ('#'-style comments were added later.)  The funny thing about ':'
comments is they get evaluated and have a return value, unlike '#' comments
which are merely whitespace.

For example:

: Arbitrary text here > foop

This "comment" wipes out the file foop, if it exists, then creates a null
file called "foop".  (Wonder why they added '#' comments? ;^)  The shell
does not attempt to execute a command called "Arbitrary", and the comment
has a return value of zero (a.k.a. success).

':' statements have other uses besides a filler with a return value of '0'
and an obscure way to create null files.

: ${VARIABLE=value}

Sets variable VARIABLE to "value" unless VARIABLE is already set.  Lastly,
beginning a sh script with ":" instead of "#! /bin/sh" saves an unnecessary
fork and exec on some versions of UNIX.  (Note to bash users: In bash,
scripts beginning with ":" are interpreted by bash while "#! /bin/sh"
scripts are, of course, executed by "/bin/sh".)


To answer your original question:

for last;do :;done

The ";done" part of the statement is a comment, which yields a syntax
error.  (sh error messages are often misleading, IMHO.)

John> One of the real hassles I keep finding with /bin/sh (and /bin/csh is
John> even worse ;-) is that the actual syntax regarding things like white
John> space, newlines, and semicolons seems to be a secret.

Oh, that reminds me.  You're not to repeat anything I've said.  :-)
I agree that csh is worse, mostly due to its (mis)handling of newlines.  I
still use csh interactively, but program in sh after being burned a few
times by csh's unexpected syntax exceptions and variations.  (Just try
piping the result of one loop into another loop in csh without using a
temporary file, FIFO or multiple invocations of csh.)

#include <std/disclaimer.h>

	Hope this helps,
--
Chuck Phillips  MS440
NCR Microelectronics 			chuck.phillips%ftcollins.ncr.com
2001 Danfield Ct.
Ft. Collins, CO.  80525   		...uunet!ncrlnk!ncr-mpd!bach!chuckp



More information about the Comp.unix.shell mailing list