Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!hubcap!ncrcae!ncrlnk!ncr-mpd!Chuck.Phillips From: Chuck.Phillips@FtCollins.NCR.COM (Chuck.Phillips) Newsgroups: comp.unix.shell,fc.unix Subject: Re: Finding the last arg Message-ID: Date: 14 Jan 91 15:43:04 GMT References: <18476@shlump.nac.dec.com> <1990Dec27.060903.1604@onion.pdx.com> <1020@mwtech.UUCP> <443@minya.UUCP> Sender: uucp@ncr-mpd.FtCollins.NCR.COM Organization: NCR Microelectronics, Ft. Collins, CO Lines: 63 In-reply-to: jc@minya.UUCP's message of 5 Jan 91 15:56:45 GMT >>>>> On 5 Jan 91 15:56:45 GMT, jc@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 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