akbloom@aplcen.apl.jhu.edu (Keith Bloom) (04/11/89)
I use a SysV clone (Venix) and BSD 4.2. My login shell on both systems is Bourne shell. Recently I took a shell script which worked fine under Venix and moved it to BSD. When I ran the script, I got the message "if: Expression syntax" Based on a hunch (recollections of other scripts) I inserted a ":" at the top of the script (before the "if"). It worked. I also tried inserting "#! /bin/sh", which also worked. So the problem is solved, but I'd still like to understand why this was necessary. Everything I've read about the : says simply that it returns true - and so it does. But apparently that's not all it does. Also, are there any differences between using : and using #! /bin/sh ? (Before you tell me to RTFM: the BSD documentation I inherited is very disorganized, and parts of it are not there at all) -- Keith Bloom UUCP: {rutgers|ames|uunet}!mimsy!apclen!akbloom Internet: akbloom@aplcen.apl.jhu.edu
chris@mimsy.UUCP (Chris Torek) (04/11/89)
In article <1005@aplcen.apl.jhu.edu> akbloom@aplcen.apl.jhu.edu (Keith Bloom) writes: >I use a SysV clone (Venix) and BSD 4.2. My login shell on both >systems is Bourne shell. Recently I took a shell script which >worked fine under Venix and moved it to BSD. When I ran the script, >I got the message "if: Expression syntax" [followed by more lines, essentially asking `why?'] (rummage, rummage ... Aha:) From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.unix-wizards Subject: Re: # sign in shell/cshell scripts... Date: 17 Apr 86 20:30:30 GMT Once upon a time, there was the shell. Since there was only `the' shell, there was no trouble deciding how to run a script: run it with *the* shell. It worked, and everyone was happy. Along came progress, and wrote another shell. The people thought this was good, for now they could choose their own shells. So some chose the one, and some the other, and they wrote shell scripts and were happy. But one day someone who used the `other' shell ran a script by someone who use the `other other' shell, and alas! it bombed spectacularly. The people wailed and called upon their Guru for help. `Well,' said the Guru, `I see the problem. The one shell and the other are not compatible. We must ensure that the shells know which other shell to use to run each script. And lo! the one shell has a ``comment'' called ``:'', and the other a true comment called ``#''. I hereby decree that henceforth, the one shell will run scripts that start with ``:'', and the other those that start with ``#''.' And it was so, and the people were happy. But progress was not finished. This time he noticed that only shells ran scripts, and thought that if the kernel too could run scripts, that this would be good, and the people would be happy. So he wrote more code, and now the kernel could run scripts, but only if they began with the magic incantation: `#!', and told the kernel which shell ran the script. And it was so, and the people were confused. For the `#!' looked like a `comment'. Though the kernel could see the `#!' and run a shell, it would not do so unless certain magic bits were set. And if the incantation were mispronounced, that too could stop the kernel, which after all was not omniscient. And so the people wailed, but alas!, the Guru did not respond. And so it was, and still it is today. * * * * * Anyway, you will get best results from a 4BSD machine by using #! /bin/sh or #! /bin/csh as the first line of your script. `#! /bin/csh -f' is also helpful on occasion. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris