[comp.unix.wizards] Shell/Cshell questions

ncsrini@ndsuvax.UUCP (srini) (08/21/88)

Hello. Some questions on the working of the shell. I am using C shell.

1. When a variable is set, where and how is it kept track of?  Like, is
   there a symbol table maintained by the shell. Or, is it something
   else. In any case, can the user access these tables to find out
   about the variables? The answer to this would probably expplain

   the mechanism of the command "set". 

2. Where are the environment variables stored? Is it in the same pplace
   as other variables or not?

3. What is the exact use of .cshrc and .login? Can one replace the other?
   If so, what are the side effects? How are the subshells affected by these
   files?

4. Can somebody recommend a book or an article that deals with the working   
   of the shell? I am looking for one that talks about Cshell syntax in
   detail, also.

5. Where are the pprograms (system) for different commands supppported by
   a shell usually located in a system? I looked in /bin etc, but couldn't
   find them.


   I guess this is enough for now. Let me add that there could be some
   silly questions above due to the fact that I am not a pro in shell
   programming or anything. But, your responses to all or some of the
   above will be greatly appreciated whether sent directly to me or to
   the net. Thank you all in advance.



-- Srini

  (ncsrini at ndsuvax or ncsrini at plains.NoDak.edu  )
  (nu104046 at ndsuvm1  -- BITNET)

ferg@koko.UUCP (John Ferguson) (08/21/88)

In article <1145@ndsuvax.UUCP> ncsrini@ndsuvax.UUCP (srini) writes:
>
>Hello. Some questions on the working of the shell. I am using C shell.
>
>1. When a variable is set, where and how is it kept track of?  Like, is
>   there a symbol table maintained by the shell. Or, is it something
>   else. In any case, can the user access these tables to find out
>   about the variables? The answer to this would probably expplain
>
>   the mechanism of the command "set". 
>
Local shell variables (i.e. those manipulated by 'set' and 'unset') are
kept in dynamically allocated memory by the shell (I can't recall if they
are in a list, tree, whatever -- it isn't important to us).  The only
way to get at them is with the shells builtin commands.  When the command
line is parsed, the shell checks to see if any of the stuff it sees is a
shell variable, alias, shell function (Bourne, KSH), or environment
variable, then does simple substitution.

>2. Where are the environment variables stored? Is it in the same pplace
>   as other variables or not?
>
When a C program starts up, there are actually 3 args to main: argc, argv,
and envp, a pointer to the environment variables.  To quote the V7 manual:
"The shell sh(1) passes an environment entry for each global shell variable
defined when the program is called".  Envp is a pointer to an array of
strings of the form: var=blah.  Check the man pages for exec(2) to see how
to pass the environment pointer to other programs.

>3. What is the exact use of .cshrc and .login? Can one replace the other?
>   If so, what are the side effects? How are the subshells affected by these
>   files?
>
When Csh starts up, it sources the commands in both .cshrc and .login.
Normally .login contains setup information you wish to be active during
the entire life of your login session, such as TERM, PATH, ...  The
.cshrc file is examined for each subsequent invocation of csh, so you
put stuff there which needs to be modified or reset when a subshell
starts up.  Someone else should be able to add more to this...

>4. Can somebody recommend a book or an article that deals with the working   
>   of the shell? I am looking for one that talks about Cshell syntax in
>   detail, also.
>
The UNIX(tm) Shell Programming Language by Manis & Meyer (Sams, 1986) has
some information of csh syntax.  What's the name of that book on csh?  I
personally prefer the csh man pages, which give the whole story, perhaps
more readable than some other man pages ;-)

>5. Where are the pprograms (system) for different commands supppported by
>   a shell usually located in a system? I looked in /bin etc, but couldn't
>   find them.
>
You probably are referring to shell builtins.  I once looked all over for
'cd'.  The shell (whatever one you choose) has certain commands which are
builtin for speed or necessity.  Even though you use them as any other
UNIX command, no subshell is executed.  The work is done by shell code.

   ferg

bd@hpsemc.HP.COM (bob desinger) (08/22/88)

ncsrini@ndsuvax.UUCP (srini)asks and John Ferguson (ferg@koko.UUCP) replies:
> >5. Where are the pprograms (system) for different commands supppported by
> >   a shell usually located in a system?
> You probably are referring to shell builtins.

Built-in commands recognized by csh are duplicated as zero-length
files in /usr/lib/builtins/.  (There is no corresponding directory for
sh or ksh; the files in builtins/ are strictly for csh.)

-- bd

swilson%thetone@Sun.COM (Scott Wilson) (08/23/88)

>When Csh starts up, it sources the commands in both .cshrc and .login.
>Normally .login contains setup information you wish to be active during
>the entire life of your login session, such as TERM, PATH, ...  The
>.cshrc file is examined for each subsequent invocation of csh, so you
>put stuff there which needs to be modified or reset when a subshell
>starts up.  Someone else should be able to add more to this...

In the old days when everyone just logged into a terminal it was easier
to decide whether settings of various things should go in .login or
.cshrc.  However, in the days of networked window systems things can get
a little messier.  At the last place I worked it was quite common to
use rsh to start an xterm running csh on a remote machine with output
directed to the local machine's X server.  This gives you the strange
situation of having a csh that is not the child of a login shell.
Therefore you had to set things like environment variables that
would otherwise be inherited from a login shell in .cshrc.

--
Scott Wilson		arpa: swilson@sun.com
Sun Microsystems	uucp: ...!sun!swilson
Mt. View, CA

chet@pirate.CWRU.EDU (Chet Ramey) (08/23/88)

In article <839@koko.UUCP> ferg@koko.UUCP (John Ferguson) writes:
>In article <1145@ndsuvax.UUCP> ncsrini@ndsuvax.UUCP (srini) writes:
>>Hello. Some questions on the working of the shell. I am using C shell.

	[Some stuff about the C-shell]

>>3. What is the exact use of .cshrc and .login? Can one replace the other?
>>   If so, what are the side effects? How are the subshells affected by these
>>   files?

>When Csh starts up, it sources the commands in both .cshrc and .login.

Not every time it starts up -- see below.

>Normally .login contains setup information you wish to be active during
>the entire life of your login session, such as TERM, PATH, ...  The
>.cshrc file is examined for each subsequent invocation of csh, so you
>put stuff there which needs to be modified or reset when a subshell
>starts up.  Someone else should be able to add more to this...

The .login is not sourced on every csh startup (e.g. when starting a new
xterm window), but only those for which the first char of argv[0] is a
hyphen (this denotes a login shell).  Csh can, however, be told to source
the .login file by having the first char be a hyphen.

Chet Ramey
chet@cwjcc.CWRU.EDU


| Chet Ramey            chet@cwjcc.CWRU.EDU    chet@alpha.CES.CWRU.EDU
|
|	"It is, it is a glorious thing to be a pirate king"

serge@imag.imag.fr (Serge Rouveyrol) (08/23/88)

>The UNIX(tm) Shell Programming Language by Manis & Meyer (Sams, 1986) has
>some information of csh syntax.  What's the name of that book on csh?  

The UNIX(tm) C SHELL fiel Guide by Anderson & Anderson (Prentice Hall, 1986)
-- 
"Les jours de pluie ...  etc ... "
serge@imag.imag.fr      serge@imag.UUCP      uunet.uu.net!imag!serge
Rouveyrol Serge,Laboratoire L.G.I,BP 53,38041 Grenoble , CEDEX , FRANCE

bd@hpsemc.HP.COM (bob desinger) (08/26/88)

Yow!  John Nelson called my bluff on /usr/lib/builtins:  he said
he couldn't find it on any of the machines that he has access to.
I remembered it from 4.2BSD (and HP-UX picked it up; probably some
other vendors whose implementations are based on 4.2 have it as well).
But I just checked and it's sure not on 4.3BSD.  So /usr/lib/builtins
isn't quite so widespread.

-- bd

mouse@mcgill-vision.UUCP (der Mouse) (08/26/88)

In article <570020@hpsemc.HP.COM>, bd@hpsemc.HP.COM (bob desinger) writes:
> ncsrini@ndsuvax.UUCP (srini)asks and John Ferguson (ferg@koko.UUCP) replies:
[an attribution seems to have been lost -dM]
>>> 5. Where are the pprograms (system) for different commands
>>>    supppported by a shell usually located in a system?
>> You probably are referring to shell builtins.
> Built-in commands recognized by csh are duplicated as zero-length
> files in /usr/lib/builtins/.  (There is no corresponding directory
> for sh or ksh; the files in builtins/ are strictly for csh.)

Please, people, qualify questions and answers with a system type, or at
least general family.  I've never heard of /usr/lib/builtins and
neither have our machines (most of them BSD).  (Unless, of course, the
question or answer is inherently specific, such as "what's the BSD
analog of VMIN and VTIME".)

As far as I know, BSD doesn't have files anywhere corresponding to
shell builtins (sh, csh, foosh, it matters not).  Some things which are
built into some shells but not others exist as programs for the benefit
of the shells into which they are not built, but that's about it.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

(To stave off the flood: BSD has no direct analogs of VMIN and VTIME.)

karl@triceratops.cis.ohio-state.edu (Karl Kleinpaste) (08/30/88)

bd@hpsemc.hp.com writes:
   Yow!  John Nelson called my bluff on /usr/lib/builtins:  he said
   he couldn't find it on any of the machines that he has access to.
   I remembered it from 4.2BSD (and HP-UX picked it up; probably some
   other vendors whose implementations are based on 4.2 have it as well).

You have your direction backwards: The set of changes to csh which
employ things like /usr/lib/builtins *originated* at HP via CMU.  The
base of such changes was the tenex.c module distributed around Usenet
in October 1983 by Ken Greer of HP Labs, which includes the comment:

/*
 * Tenex style file name recognition, .. and more.
 * History:
 *      Author: Ken Greer, Sept. 1975, CMU.
 *      Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
 *
 *      Search and recognition of command names (in addition to file names)
 *      by Mike Ellis, Fairchild A.I. Labs, Sept 1983.
 */

--Karl

bd@hpsemc.HP.COM (bob desinger) (08/31/88)

Karl Kleinpaste (karl@triceratops.cis.ohio-state.edu) notes that
/usr/lib/builtins belongs to the Tenex csh.  Indeed, we were running
tcsh on our Vax 11/750 running 4.2BSD!  Thanks for the correction.
To der Mouse:  sorry; I'm beating myself with a hammer right now.

(BTW, Ken Greer is no longer at HP Labs.  He's the driving force
behind Elan Computer Group, the troff folks.)

-- bd