[comp.unix.questions] Which shell language should I use?

gsg0384@uxa.cso.uiuc.edu (04/26/89)

Hi,

I simply thought that I should use the C-shell script language since
I am using /bin/csh as an interactive shell.  But
I found, and was surprised, that most shell script files are Bourn-shell 
script files.  Even some of the Berkely-originated source were
wriiten in Bourn-shell language.

1. How much of the Berkeley-originated script files were written 
in Bourn-shell language?

  It seems to me that such practice enhances
portability.  I'd like to hear some opinion on which shell-sript
language is preferable for what purpose.

2. I found that the first line of a shell-script file is special
   even though it contains '#'.  Sometimes I found, and sometimes not,

#!/bin/csh

  or

#!/bin/sh

   in the first line.  They looks like directives.  Am I right?

3.  Where in the UNIX manual can I find some kind of rule on which
   shell-script parser is applied for a certain shell-script file?

4. Please recommend a book on shell programming.

Thanks a lot.

             Hugh
             gsg0384@uxa.cso.uiuc.edu

karish@forel.stanford.edu (Chuck Karish) (04/29/89)

In article <113700004@uxa.cso.uiuc.edu> gsg0384@uxa.cso.uiuc.edu wrote:
>1. How much of the Berkeley-originated script files were written 
>in Bourn-shell language?

	A good exercise for the original poster.  Write a script
	to look for csh syntax/magic cookies in all the scripts
	on your machine.  I'd use `find', `file', and `sed' to
	do it.

>  It seems to me that such practice enhances
>portability.  I'd like to hear some opinion on which shell-sript
>language is preferable for what purpose.

	I agree.  csh is not present on all systems, and differs
	from implementation to implementation, mostly because
	a few vendors ship ancient versions.

	The most portable scripts use the subset of the modern
	sh language that's supported by the V7/BSD sh program.

	sh is a better language than csh for writing complex
	programs.  Its quoting syntax is less idiosyncratic,
	and its flow-of-control operators work with pipes
	and redirection.  In sh, it's possible to redirect the
	output of a `while' loop.  In csh, you'd have to create
	a subshell yourself.  Similarly, sh lets you redirect
	different output streams of the same command to different
	files; under csh, you'd have to use another subshell.

>2. I found that the first line of a shell-script file is special
>   even though it contains '#'.  Sometimes I found, and sometimes not,

>#!/bin/csh ...

	Some systems read the first line, and use this as the
	name of the interpretor to use.  Some just recognize
	the '#' as the first character in the file as a signal
	to use csh.

	The default is to use sh, if no magic cookie at all
	is found.

	Does not work at all unless the script is invoked from
	execlp() or execvp().

>3.  Where in the UNIX manual can I find some kind of rule on which
>   shell-script parser is applied for a certain shell-script file?

	execl(3), execve(2)

	Chuck Karish		{decwrl,hpda}!mindcrf!karish
	(415) 493-7277		karish@forel.stanford.edu

fyl@ssc.UUCP (Phil Hughes) (05/02/89)

In article <113700004@uxa.cso.uiuc.edu>, gsg0384@uxa.cso.uiuc.edu writes:

> I simply thought that I should use the C-shell script language since
> I am using /bin/csh as an interactive shell.  But
> I found, and was surprised, that most shell script files are Bourn-shell 
> script files.  Even some of the Berkely-originated source were
> wriiten in Bourn-shell language.

The cute answer is "use Bourne shell" so you can buy our tutorial on it.
:-)  But, there is more.
Generally, scripts are written using Bourne shell because it is portable
(available on all UNIX systems) and generally executes faster.  There is
the short answer.  Now, here is what we have done at SSC.

Almost all of our shell scripts are written using the Bourne shell.
The exceptions are scripts where having arrays available within the shell
made the job much less complicated.  On the other hand, almost all of out
users use csh interactively.

I teach two different classes where the students do shell work.  In the
more general one (called UNIX for Programmers but it is just UNIX for
technically oriented people) I teach shell programming using csh.  I made
this decision because I felt it was easier to explain some concepts using
csh.  For example, the relationship between the shell and the environment
and dealing with arithmetic on strings.  The other class, which is a 1-day
seminar on writing (as much as I hate to use this word) business
applications using UNIX tools is taught using Bourne shell.  The
difference is that in UFP I am showing concepts and at the end of the
sction on csh I do a comparison between the shells.  In the applications
class I am really teaching the use of the utilities and how to use the
shell as "glue".  I expect people to take the examples from this class and
apply them directly to solve a problem.

In other words, here are some ideas but you need to take your pick.

> 1. How much of the Berkeley-originated script files were written 
> in Bourn-shell language?
My guess -- most.

> 2. I found that the first line of a shell-script file is special
>    even though it contains '#'.  Sometimes I found, and sometimes not,
> 
> #!/bin/csh
> 
>   or
> 
> #!/bin/sh
> 
>    in the first line.  They looks like directives.  Am I right?
Yes.  Not all shells understand these (XENIX being the biggest example).
But, if your shell is smart enough to undersand it is a directive to the
shell that /bin/csh or /bin/sh is the correct program to interpret what
follows.  Note that this could be the name of any program, not just the
Bourne or C shell.

> 3.  Where in the UNIX manual can I find some kind of rule on which
>    shell-script parser is applied for a certain shell-script file?
Your SHELL environment variable determines which shell is invoked to
interpret a script.  If this is csh and the first character of the script
is not a #, csh passes the script to the Bourne shell.

> 4. Please recommend a book on shell programming.
UNIX Shell Programming by Kochan and Wood is fairly comprehensive.
For C shell, The C Shell Field Guide by Anderson and Anderson is
excellent.  And, for a pocket guide to the Bourne shell, we (SSC)
publishes the Bourne Shell Pocket Tutorial.

Good luck.


-- 
Phil Hughes, SSC, Inc. P.O. Box 55549, Seattle, WA 98155  (206)FOR-UNIX
    uw-beaver!tikal!ssc!fyl or uunet!pilchuck!ssc!fyl or attmail!ssc!fyl

guy@auspex.auspex.com (Guy Harris) (05/03/89)

>> 1. How much of the Berkeley-originated script files were written 
>> in Bourn-shell language?
>My guess -- most.

I checked the ones in the directories under "/usr/src", looking for ".sh"
and ".csh" source files, and the former outnumbered the latter 17 to 1
in the 4.3-tahoe source.  This at least indicates that most are written
in Bourne shell.

>>[stuff about #!]

>Yes.  Not all shells understand these (XENIX being the biggest
>example).

4.x BSD being another example - it's not understood by the shell, it's
understood by the 4.xBSD kernel, which implements the "exec" calls.  In
4.xBSD and in most systems that have picked up "#!", putting in a "#!"
line makes a script for some program executable in the same way that a
program is - plain old boring "exec" calls can run it.  Some systems may
have implemented it in the shell.

>> 3.  Where in the UNIX manual can I find some kind of rule on which
>>    shell-script parser is applied for a certain shell-script file?
>Your SHELL environment variable determines which shell is invoked to
>interpret a script.

Well, sort of, in the sense that the shell at which you type the name of
the shell script file determines which shell is used to interpret the
script, and the SHELL environment variable is set to the name of that
shell.  Merely changing the SHELL environment variable won't necessarily
change the shell used to interpret the script, however. 

jeffrey@algor2.UUCP (Jeffrey Kegler) (05/06/89)

First, every UNIX programmer should know how to use plain old sh (and ed, for
that matter).  Every once in a while you have no other choice.  So if you
do not know sh, that is the one to learn first.

Which should be your shell of preference is more a matter of taste.  Before
the Korn shell came along, I was a big user of csh.  The major problem with
csh is that it is not Bourne shell (that is, sh) compatible.  I wind up moving
around a lot and changed shell syntax has gotten to be too much trouble for
me.  Now I never bother with csh any more, and use the Korn shell if I have
it, the Bourne shell if I do not.

One of the main reasons I stopped using csh is the issue of shell scripts.  If
you desire portability, it is risky to write a shell script in anything but
the Bourne shell.  Also, for heavy duty shell programming, the Bourne and
Korn shells are better than csh.  Not to go into details, but redirection of
input and output from while and for constructs in very useful, and not 
available in csh.

To summarize:
1.) If you do not know the Bourne shell, sh, learn it before doing anything
else.
2.) Pick an interactive shell of preference.  If you are reasonably sane, it
will be ksh or csh.  I prefer ksh.
3.) Always write shell scripts in sh.
-- 

Jeffrey Kegler, President, Algorists,
jeffrey@algor2.UU.NET or uunet!algor2!jeffrey
1762 Wainwright DR, Reston VA 22090