[comp.unix.questions] Bourne Shell Comments Problem

nab1382@dsacng1.UUCP (Dick Hauser) (09/21/88)

I have a question regarding usage of a comment line in the Bourne
Shell.  Here is the situation.  The comment indicator (i.e #) is
in position 1 of the first record of the file.  The comment line
was followed by a read command for a varibale.  When the shell  
was executed using "sh -x shellname", everything worked.  But
when the shell is executed, and execution is not traced, an error
message "read not found" is displayed.

I have exhausted available resources, and cannot determine what is
causing the problem.  If you have the answer, let me know.

		    Thanks in advance for any assistance
-- 
Dick Hauser        PHONE:  +1616-961-5312          AV: 932-5312       
Defense Systems Automation Center, Battle Creek, Mi.
UUCP:   {uunet!gould,cbosgd!osu-cis}!dsacg1!dsacng1dhauser      
        ...eecae!dsacng1!dhauser 

debra@alice.UUCP (Paul De Bra) (09/22/88)

In article <292@dsacng1.UUCP> nab1382@dsacng1.UUCP (Dick Hauser) writes:
>I have a question regarding usage of a comment line in the Bourne
>Shell.  Here is the situation.  The comment indicator (i.e #) is
>in position 1 of the first record of the file.  The comment line
>was followed by a read command for a varibale.  When the shell  
>was executed using "sh -x shellname", everything worked.  But
>when the shell is executed, and execution is not traced, an error
>message "read not found" is displayed.

On some Unix systems instead of looking for the magic number #!
the kernel only looks for # to find out whether a file is a shell
script, possibly for another shell.
So the # sign on position 1 of the first line of a file is not REALLY
a comment indicator on these odd unix systems.

Easy fix: start with an empty line first and start the comment on line 2.

Paul.

meo@stiatl.UUCP (Miles O'Neal) (09/22/88)

In article <292@dsacng1.UUCP>, nab1382@dsacng1.UUCP (Dick Hauser) writes:
> The comment indicator (i.e #) is
> in position 1 of the first record of the file.  The comment line
> was followed by a read command for a varibale.  When the shell  
> was executed using "sh -x shellname", everything worked.  But
> when the shell is executed, and execution is not traced, an error
> message "read not found" is displayed.

A comment indicator in column1, line 1, indicates the script is
a csh script instead of sh. Bourne shell (sh) scripts with comments
at the top require a blank line (or anything other than a comment)
on the first line. Since read is built into sh, and csh uses a
different mechanism for reading input, csh could not find a read
program in your path to invoke.

As the above implies, any script that does not have the comment starting
in column 1, line 1, will default to sh.

I am not familiar with ksh (barbarian that I am), so I don't know how
that fits into the scheme of things. I also don't know what happens
on System V implementations without a vendor-added or PD csh.

quan@hplabsb.UUCP (Suu Quan) (09/22/88)

In article <292@dsacng1.UUCP>, nab1382@dsacng1.UUCP (Dick Hauser) writes:
> I have a question regarding usage of a comment line in the Bourne
> Shell.  Here is the situation.  The comment indicator (i.e #) is
> in position 1 of the first record of the file.  The comment line
> was followed by a read command for a varibale.  When the shell  
> was executed using "sh -x shellname", everything worked.  But
> when the shell is executed, and execution is not traced, an error
> message "read not found" is displayed.
> 
	I bet your shell script will work with "sh shellname".
Doing either "sh -x shellname" or "sh shellname" forces the file
to be interpreted by the Bourne shell. 

	Usually, a # as the very first character of a file is NOT a
comment, but an indication of what shell should be used to interpret this.

	I have the habit to use as 1st line :
"#! /bin/sh"
or
"#! /bin/csh"

	Everything else (as 1st character) implies using your favorite shell.

NOTE : " #! /bin/csh" as the first line will be executed by your favorite
	shell, not csh : 1st character is blank.

morrison@ficc.uu.net (brad morrison XNX SE#) (09/23/88)

In article <292@dsacng1.UUCP> nab1382@dsacng1.UUCP (Dick Hauser) writes:
>I have a question regarding usage of a comment line in the Bourne
>Shell.  Here is the situation.  The comment indicator (i.e #) is
>in position 1 of the first record of the file.  The comment line
>was followed by a read command for a variable.  When the shell  
>was executed using "sh -x shellname", everything worked.  But
>when the shell is executed, and execution is not traced, an error
>message "read not found" is displayed.

Sounds like you have a Cshell running the Bourne shell script.
Csh assumes that the script is to be run with a csh child if the
first character is '#'.  Even though '#' is the comment indicator
for both shells, csh gives it additional (and sometimes inconvenient)
meaning.
-- 
Brad Morrison                 Ferranti International Controls Corporation
phone: (713) 274-5449                          12808 W. Airport Boulevard
UUCP:  uunet!ficc!morrison (morrison@ficc.uu.net)    Sugar Land, TX 77478
------------ grep me no patterns and I'll tell you no lines -------------

ssi@usfvax2.EDU (Ssi) (09/23/88)

>> The fist character on the first line of a shell script is '#'
>> My  Bourne Shell script does not run right.

The problem is just that. Placing a '#' there means you want to
run the script in the csh shell. read is not a csh command.
The rest of what's in your script probably isn't either.
I think you can start your script with :
#!/bin/sh      
and it will run as you would think.
Or you can put a dummy command on line 1.

                      Greg Ripp Ph (813)628-6100

gwyn@smoke.ARPA (Doug Gwyn ) (09/23/88)

In article <375@stiatl.UUCP> meo@stiatl.UUCP (Miles O'Neal) writes:
>In article <292@dsacng1.UUCP>, nab1382@dsacng1.UUCP (Dick Hauser) writes:
>A comment indicator in column1, line 1, indicates the script is
>a csh script instead of sh.

Any Bourne shell that acts this way is BROKEN.
Nearly all Bourne shell scripts I have encountered in the past
several years start with #.

chris@mimsy.UUCP (Chris Torek) (09/23/88)

In article <1133@usfvax2.EDU> ssi@usfvax2.EDU (Ssi) writes:
>The problem is just that. Placing a '#' there means you want to
>run the script in the csh shell.

Indeed.  As you mortals say :-), what goes around, comes around.

(Message save:98)
Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site maryland.UUCP
Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site umcp-cs.UUCP
Path: maryland!umcp-cs!chris
From: chris@umcp-cs.UUCP (Chris Torek)
Newsgroups: net.unix-wizards
Subject: Re: # sign in shell/cshell scripts...
Message-ID: <995@umcp-cs.UUCP>
Date: 17 Apr 86 20:30:30 GMT
Date-Received: 17 Apr 86 20:30:30 GMT
References: <237@Shasta.ARPA>
Reply-To: chris@maryland.UUCP (Chris Torek)
Organization: University of Maryland, Dept. of Computer Sci.
Lines: 57

In article <237@Shasta.ARPA> ALEX@SU-SIERRA.ARPA writes:
>	I'm encountering weird problems with sh (and sometimes csh)
>dying on some shell scripts depending on whether or not my first line in
>the script is a comment like:
># This script does this (but not that)

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 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

And a recent addendum: System III and System V, and systems derived
from them, usually do *not* support `#!'.  Alas, if you use it, you
may get the wrong shell.  Of course, System III and System V do not
come with the C shell either.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

hollombe@ttidca.TTI.COM (The Polymath) (09/24/88)

In article <292@dsacng1.UUCP> nab1382@dsacng1.UUCP (Dick Hauser) writes:
}... The comment indicator (i.e #) is
}in position 1 of the first record of the file.  The comment line
}was followed by a read command for a varibale.  When the shell  
}was executed using "sh -x shellname", everything worked.  But
}when the shell is executed, and execution is not traced, an error
}message "read not found" is displayed.

Sounds like you're executing the wrong shell.  Try using ": #" instead of
"#" for your comments (especially on line 1).  This should force Bourne
shell execution.

-- 
The Polymath (aka: Jerry Hollombe, hollombe@ttidca.tti.com)  Illegitimati Nil
Citicorp(+)TTI                                                 Carborundum
3100 Ocean Park Blvd.   (213) 452-9191, x2483
Santa Monica, CA  90405 {csun|philabs|psivax}!ttidca!hollombe

peter@ficc.uu.net (Peter da Silva) (09/27/88)

In article <8567@smoke.ARPA>, gwyn@smoke.ARPA (Doug Gwyn ) writes:
> In article <375@stiatl.UUCP> meo@stiatl.UUCP (Miles O'Neal) writes:
> >A comment indicator in column1, line 1, indicates the script is
> >a csh script instead of sh.

> Any Bourne shell that acts this way is BROKEN.
> Nearly all Bourne shell scripts I have encountered in the past
> several years start with #.

You must not have used systems with the csh available then, since csh (not
sh) uses the first character to determine whether to pass the script off onto
the bourne shell. Some might ask whether this is broken, since after all anyone
writing csh scripts has to be off their nut, but it is a fact of life. If you
have csh, check your bourne shell scripts to make sure they're csh proof.

Once upon a time a system administrator who shall not be named put # at the
front of all the bourne shell scripts on one of the Berkeley machines. It
was not a pretty sight.
-- 
Peter da Silva  `-_-'  Ferranti International Controls Corporation.
"Have you hugged  U  your wolf today?"            peter@ficc.uu.net

steve@iccdev.UUCP (Steve Clamage) (09/29/88)

In article <1628@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
 [Discussion of #! at the beginning of shell scripts deleted]
>
>You must not have used systems with the csh available then, since csh (not
>sh) uses the first character to determine whether to pass the script off onto
>the bourne shell. Some might ask whether this is broken, since after all anyone
>writing csh scripts has to be off their nut, but it is a fact of life. If you
>have csh, check your bourne shell scripts to make sure they're csh proof.

>Peter da Silva  `-_-'  Ferranti International Controls Corporation.
>"Have you hugged  U  your wolf today?"            peter@ficc.uu.net

  Actually, the #! at the beginning of a file is not a feature of csh,
it is a feature of exec()! Do a 'man exec' and you'll find out that it
looks at the first two bytes of the exec'ed file, and if it finds them,
uses the next word as the name of the interpreter for that file. Try
something like this and you'll see what I mean:

#include <stdio.h>
main()
{
	char *argv[];
	argv[0]=NULL;
	execv("foo",argv);
}

Where "foo" is just:
#!/bin/ls -l

(Don't forget to chmod +x foo..)

  I was pretty surprised when I first found out about this, but it does
make sense in a twisted sort of UNIX-y way.

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
           Steve Clamage - Industrial Computer Corp., Atlanta, GA
                    UUCP: ...gatech!ncrats!iccdev!steve
    "But then on the other hand I might/Keep the wax/And melt it down.."
                DISCLAIMER: Disclaimer? I don't even know 'er!
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

gwyn@smoke.ARPA (Doug Gwyn ) (10/03/88)

In article <1628@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>In article <8567@smoke.ARPA>, gwyn@smoke.ARPA (Doug Gwyn ) writes:
>> Nearly all Bourne shell scripts I have encountered in the past
>> several years start with #.
>You must not have used systems with the csh available then, since csh (not
>sh) uses the first character to determine whether to pass the script off onto
>the bourne shell.

Csh on old BSD systems did this, since # was not a general comment
character in the really old version of the Bourne shell that they used.
Later a hack was added to permit a limited form of # comments in Bourne
shell scripts on BSD systems.  UNIX System V has always had general #
comments in Bourne shell scripts, and most scripts start with comments
describing what the script is for, etc.

However, a port of csh to UNIX System V should NOT "exec" scripts using
csh, and a Bourne shell even on BSD should never do this.  On BSD systems,
the #! kludge can be used to force the right interpreter; on UNIX System V
this is not usually supported, and since csh is not standard on UNIX
System V in that environment shell scripts should always be executed by a
Bourne shell (except for explicit "csh <foo" or "csh foo", of course).

I notice that few porters of csh to a UNIX System V environment seem to
be familiar enough with the UNIX System V environment to fix such
incompatibilities when they do the port.

cratz@datack.UUCP (Tony Cratz) (10/04/88)

In article <8608@smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <1628@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>>In article <8567@smoke.ARPA>, gwyn@smoke.ARPA (Doug Gwyn ) writes:
>>> Nearly all Bourne shell scripts I have encountered in the past
>>> several years start with #.
>>You must not have used systems with the csh available then, since csh (not
>>sh) uses the first character to determine whether to pass the script off onto
>>the bourne shell.
			.
			.
			.
>However, a port of csh to UNIX System V should NOT "exec" scripts using
>csh, and a Bourne shell even on BSD should never do this.  On BSD systems,
>the #! kludge can be used to force the right interpreter; on UNIX System V
>this is not usually supported, and since csh is not standard on UNIX
>System V in that environment shell scripts should always be executed by a
>Bourne shell (except for explicit "csh <foo" or "csh foo", of course).
>
>I notice that few porters of csh to a UNIX System V environment seem to
>be familiar enough with the UNIX System V environment to fix such
>incompatibilities when they do the port.


I have found that on the different sytems that I have worked on so far that
under SYS V.2 that if you start your Bourne Shell scripts with the bourne
shell comment ':' that it will always run the shell as bourne. I had to
find some way to port shell scripts from BSD to SYS V.2 that I could be
be sure of working. Now all of my Bourne shell scripts start with the
following line
: !/bin/sh (run script as Broune Shell)




-- 
			"Looks like plant food to me"

Tony Cratz 	work phone: (408) 982-3585
UUCP: uunet!altnet!datack!cratz
Snail: Datachecker, 800 Central Expressway MS 33-36, Santa Clara, Ca 95052