[X-Unix] BASH problem?

William H. Magill magill at mcgillsociety.org
Mon Dec 13 11:31:28 PST 2004


On 13 Dec, 2004, at 06:22, Russell McGaha wrote:

> Actually yes and no; I am relatively new to command line programming.  
> After I chmoded the file it ran [is running] just fine as a cron job, 
> and before I changed my shell to bash, was running just fine as a 
> sourced file .

That is correct behavior.  That is the way in which "shell programming" 
works.

A couple of Unix concepts (quite generalized, go read a Unix text for 
more explicit explanations):

EVERY file is nothing but a string of characters. It is read starting 
at the first byte, that read progresses to the end-of-file. The "thing" 
reading the file can be a "program" or an "interpreter," typically 
called a "shell" -- tcsh, bash, zsh, ksh and sh all being valid 
"shells." Perl is another common interpreter, as are  Tcl/tk and Java.

ANY text file can be "sourced" by any interpreter -- The interpreter 
reads the file and if that file contains valid commands for that shell 
they will be "executed," that is to say, "interpreted" by the shell. If 
they are not "commands" which that interpreter understands, the 
interpreter will generate errors.

If your default shell was tcsh and you "sourced" a file containing tcsh 
commands it would work as expected. However, when you changed your 
shell to bash, and sourced that same file, bash found no valid 
commands... just as you experienced.

"Rational" programmers always put a shebang line as the first line of 
any shell script -- in that way they remind themselves WHICH shell it 
was written for, AND, more importantly, it allows that script to be 
"executed" as a "stand-alone" program.

When a file is sourced, the "shebang" line -- the first line of the 
file ("#!/bin/tcsh", "#!/bin/bash", "#!/bin/zsh") is simply ignored.  
It is treated as a comment because of the leading "#" by the shell's 
interpreter.

For a "file" to become an executable program (i.e. something with the 
execute bits set), the first line  of the file is examined by they 
system call, "exec." Exec determines just what it is being asked to 
look at, and therefore what it should do with it. A compiled binary 
file is no different than a shell script file -- they are both merely 
strings of bits and bytes. However, the beginning of the file contains 
information which tells exec exactly what format the file is in... 
compiled binary or text.
If it is a text file, the first line of the file is examined to 
determine what interpreter to invoke.
If there is no shebang line present to invoke an interpreter you will 
get lots of funny errors when you try to exec that love letter you 
wrote. But with the shebang line present, an interpreter is invoked to 
read and interpret the remaining data in the file.

There is lots of other "magic" taking place, ("man magic" for one 
example) including games played with the filename extension -- .sh, 
.tcsh, .bash, .pl, .jar, etc. -- which simply makes life "easier" for 
the end user.

"Flexability" is a key point in Unix and enunciated in Perl -- There is 
more than one correct way to do things!  Just as the tuber which grows 
underground can correctly be called a Russet, Idaho or Potato, except 
in very specific situations, the difference does not matter.

T.T.F.N.
William H. Magill
# Beige G3 - Rev A motherboard - 768 Meg
# Flat-panel iMac (2.1) 800MHz - Super Drive - 768 Meg
# PWS433a [Alpha 21164 Rev 7.2 (EV56)- 64 Meg]- Tru64 5.1a
# XP1000  [Alpha EV6]
magill at mcgillsociety.org
magill at acm.org
magill at mac.com
whmagill at gmail.com




More information about the X-Unix mailing list