Re: bash scripts and files

2008-01-09 Thread Bob McGowan

michael wrote:

On Wed, 2008-01-09 at 13:26 +0100, Dan H wrote:

On Wed, 09 Jan 2008 12:03:07 +
michael <[EMAIL PROTECTED]> wrote:


.bashrc and .bash_profile are different. They are only reasonably
invoked by a bash shell, so it is safe to assume they are written
using bash syntax. They are, after all, configuration files for
bash, so what other language would they be written in?

"reasonably"?!
and where is it stated they are different?
surely they should have the !# at the start too
it's this implicitness that upsets me!

They aren't even executable, so why should they have hash-bang?


aha, now that's a very good point! and perhaps the clearest reason:

scripts are executable and require hash-bang (strictly they don't have
to have that but it's useful to ensure they will run if they contain
shell specifics)

files associated with the innovation of a shell (such
as .bashrc, .cshrc, .bash_profile) are not shells and are not executable
and thus do not require hash-bang

M




This is just the tip of the iceberg, that is to say, the subject is a 
bit complex, requiring some knowledge of system calls, and history.  So 
this will be a bit long.


UNIX/Linux systems use the fork/exec system calls to handle execution of 
a program (binary executable).  A shell will fork a copy of itself, the 
copy gets a new PID but is otherwise the same as the parent.  The copy 
(child) then uses exec to overlay itself with the new program code.


But, if the program code is a shell script, the exec will return an 
error, which the shell will detect.  The child shell will then try to 
read the file as text, treating it as a shell script.


Two things to note in the above:  there is no way for what is found in 
the file and executed in the child, to pollute the parent; and the shell 
simply assumes the syntax is correct.  The script may be as simple as 
one line, invoking some other command (awk, sed, chmod) with arguments 
(this is how non-shell scripts were usually handled before hash/bang was 
invented).


Start up files need to pollute the shell, so they must be sourced.  This 
happens automatically for the start up files the shell knows about, and 
can be done manually by the user, using the 'dot' (.) or 'source' 
command, depending on the shell in use.


In the case of sourcing, just as for the case with the failed exec, 
above, the shell assumes the file format is its own.  And, sourced files 
do *not* need to be made executable.  Technically, scripts don't either, 
you can run them as 'shell script' without doing a chmod.


Now, the history.  This rounds out the picture, but is not really 
necessary for understanding startup scripts.


The original UNIX shell was a loner, there were no other shells, so just 
reading the file and running the commands was enough.  But then came the 
C shell and it had different syntax.  So, the C shell had an extra bit 
added to it.  Reading the first line, if it has a single character and 
that character is a colon, then the file is not a C shell script, exec a 
"standard" shell to run it.  Otherwise, treat it as a C shell script.


And this worked fine, for a time.  But then there were awk and sed, then 
Perl, Python, and so on.  And an easier way was needed to make scripts 
with these languages work.  The standard, with awk and sed, started out 
to be writing a file with the script, and running awk or sed with an 
option and script file name, similar to using 'shell script'.  But that 
wasn't good enough, too much typing.


So the exec system call was modified to deal with scripts directly.  It 
will read the first two characters of the executable file and if they 
are hash/bang, it will use the rest of the line to find the actual file 
to exec, passing that program the name of the script with any arguments.


--
Bob McGowan


smime.p7s
Description: S/MIME Cryptographic Signature


Re: bash scripts and files

2008-01-09 Thread michael
On Wed, 2008-01-09 at 13:26 +0100, Dan H wrote:
> On Wed, 09 Jan 2008 12:03:07 +
> michael <[EMAIL PROTECTED]> wrote:
> 
> > > .bashrc and .bash_profile are different. They are only reasonably
> > > invoked by a bash shell, so it is safe to assume they are written
> > > using bash syntax. They are, after all, configuration files for
> > > bash, so what other language would they be written in?
> > 
> > "reasonably"?!
> > and where is it stated they are different?
> > surely they should have the !# at the start too
> > it's this implicitness that upsets me!
> 
> They aren't even executable, so why should they have hash-bang?

aha, now that's a very good point! and perhaps the clearest reason:

scripts are executable and require hash-bang (strictly they don't have
to have that but it's useful to ensure they will run if they contain
shell specifics)

files associated with the innovation of a shell (such
as .bashrc, .cshrc, .bash_profile) are not shells and are not executable
and thus do not require hash-bang

M


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: bash scripts and files

2008-01-09 Thread Dan H
On Wed, 09 Jan 2008 12:03:07 +
michael <[EMAIL PROTECTED]> wrote:

> > .bashrc and .bash_profile are different. They are only reasonably
> > invoked by a bash shell, so it is safe to assume they are written
> > using bash syntax. They are, after all, configuration files for
> > bash, so what other language would they be written in?
> 
> "reasonably"?!
> and where is it stated they are different?
> surely they should have the !# at the start too
> it's this implicitness that upsets me!

They aren't even executable, so why should they have hash-bang?

--D.


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: bash scripts and files

2008-01-09 Thread michael
On Mon, 2007-12-31 at 18:51 +, Tyler Smith wrote:
> On 2007-12-31, michael <[EMAIL PROTECTED]> wrote:
> >
> > Thanks, probably the previous chapter ("she-bang") was of more use  
> > but a useful ref. However, I'm still trying to understand why it's  
> > not usual to have a she-bang for the .bash_profile and .bashrc files.  
> > That documentation reads as if it's expected - they are scripts and  
> > contain shell specific syntax.
> 
> Regular shell scripts could be called from any number of places. If
> you're running a bash shell, you could run a csh or zsh or python
> script. Similarly any of these scripts could be called from another
> process, such as from a program written in C, or Lisp, or whatever. In
> any of these cases the language of the script has no relation to the
> environment it is called from. All the caller knows is that they are
> executable files - any details are hidden. That means the pertinent
> information needs to be stored in the script itself, and that has to
> happen on the first line so the proper interpreter is invoked.
> 
> Imagine what would happen if you didn't do this. You call a script
> from your terminal running bash, and that script is written in Perl.
> Without the #! the terminal could either assume it's written in bash,
> and choke on the syntax, or try and guess the language, which gets
> hairy very quickly.

Yes, all this I understand and appreciate


> 
> .bashrc and .bash_profile are different. They are only reasonably
> invoked by a bash shell, so it is safe to assume they are written
> using bash syntax. They are, after all, configuration files for bash,
> so what other language would they be written in?

"reasonably"?!
and where is it stated they are different?
surely they should have the !# at the start too
it's this implicitness that upsets me!



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: bash scripts and files

2007-12-31 Thread Tyler Smith
On 2007-12-31, michael <[EMAIL PROTECTED]> wrote:
>
> Thanks, probably the previous chapter ("she-bang") was of more use  
> but a useful ref. However, I'm still trying to understand why it's  
> not usual to have a she-bang for the .bash_profile and .bashrc files.  
> That documentation reads as if it's expected - they are scripts and  
> contain shell specific syntax.

Regular shell scripts could be called from any number of places. If
you're running a bash shell, you could run a csh or zsh or python
script. Similarly any of these scripts could be called from another
process, such as from a program written in C, or Lisp, or whatever. In
any of these cases the language of the script has no relation to the
environment it is called from. All the caller knows is that they are
executable files - any details are hidden. That means the pertinent
information needs to be stored in the script itself, and that has to
happen on the first line so the proper interpreter is invoked.

Imagine what would happen if you didn't do this. You call a script
from your terminal running bash, and that script is written in Perl.
Without the #! the terminal could either assume it's written in bash,
and choke on the syntax, or try and guess the language, which gets
hairy very quickly.

.bashrc and .bash_profile are different. They are only reasonably
invoked by a bash shell, so it is safe to assume they are written
using bash syntax. They are, after all, configuration files for bash,
so what other language would they be written in?

HTH,

Tyler


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: bash scripts and files

2007-12-31 Thread Ron Johnson
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 12/31/07 05:07, michael wrote:
> 
> On 30 Dec 2007, at 18:11, Gerard Robin wrote:
> 
>> On Sun, Dec 30, 2007 at 05:17:43PM +, michael wrote:
>>> From: michael <[EMAIL PROTECTED]>
>>> To: debian user 
>>> Subject: bash scripts and files
>>>
>>> Folks, can somebody point me to an authorative reference that
>>> explains when one needs to put, eg,
>>>  #!/bin/bash
>>> as the first line of a script and whether or not it's
>>> required/surplus/ignored for bash specific files such as .bashrc and
>>> .bash_profile
>> have look at:
>> http://tldp.org/LDP/abs/html/invoking.html
>> The whole tutorial is in the package: abs-guide
> 
> Thanks, probably the previous chapter ("she-bang") was of more use but a
> useful ref. However, I'm still trying to understand why it's not usual
> to have a she-bang for the .bash_profile and .bashrc files. That
> documentation reads as if it's expected - they are scripts and contain
> shell specific syntax.

Probably because those files are "sourced" from within another
process instead of directly executable from the command line.

- --
Ron Johnson, Jr.
Jefferson LA  USA

"Your mistletoe is no match for my TOW missile."  Santa-bot
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHeSp6S9HxQb37XmcRAi/0AJ9qapUi8ZsCiOq2JY5n3IoeeJClLwCeNnLY
coeJqZYesLAESd1d7CUaspg=
=S2v4
-END PGP SIGNATURE-


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: bash scripts and files

2007-12-31 Thread michael


On 30 Dec 2007, at 18:11, Gerard Robin wrote:


On Sun, Dec 30, 2007 at 05:17:43PM +, michael wrote:

From: michael <[EMAIL PROTECTED]>
To: debian user 
Subject: bash scripts and files
X-Spam-Virus: No
X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on  
liszt.debian.org
X-Spam-Level: X-Spam-Status: No, score=-6.6 required=4.0  
tests=AWL,LDO_WHITELIST,

RCVD_IN_DNSWL_MED autolearn=failed version=3.2.3
X-Mailer: Apple Mail (2.752.3)

Folks, can somebody point me to an authorative reference that  
explains when one needs to put, eg,

 #!/bin/bash
as the first line of a script and whether or not it's required/ 
surplus/ignored for bash specific files such as .bashrc  
and .bash_profile

have look at:
http://tldp.org/LDP/abs/html/invoking.html
The whole tutorial is in the package: abs-guide


Thanks, probably the previous chapter ("she-bang") was of more use  
but a useful ref. However, I'm still trying to understand why it's  
not usual to have a she-bang for the .bash_profile and .bashrc files.  
That documentation reads as if it's expected - they are scripts and  
contain shell specific syntax.


Thanks, Michael


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]




Re: bash scripts and files

2007-12-30 Thread Gerard Robin

On Sun, Dec 30, 2007 at 05:17:43PM +, michael wrote:

From: michael <[EMAIL PROTECTED]>
To: debian user 
Subject: bash scripts and files
X-Spam-Virus: No
X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on liszt.debian.org
X-Spam-Level: 
X-Spam-Status: No, score=-6.6 required=4.0 tests=AWL,LDO_WHITELIST,

RCVD_IN_DNSWL_MED autolearn=failed version=3.2.3
X-Mailer: Apple Mail (2.752.3)

Folks, can somebody point me to an authorative reference that explains when 
one needs to put, eg,

 #!/bin/bash
as the first line of a script and whether or not it's 
required/surplus/ignored for bash specific files such as .bashrc and 
.bash_profile

have look at:
http://tldp.org/LDP/abs/html/invoking.html
The whole tutorial is in the package: abs-guide

hth.
--
Gérard



Re: bash scripts and files

2007-12-30 Thread Iñigo Tejedor Arrondo
2007/12/30, michael <[EMAIL PROTECTED]>:
> Folks, can somebody point me to an authorative reference that
> explains when one needs to put, eg,
>   #!/bin/bash
> as the first line of a script and whether or not it's required/
> surplus/ignored for bash specific files such as .bashrc
> and .bash_profile
>
> many thanks but couldn't find it quickly using 'oogle. M

In a script that line specifies the interpreter to use to execute the
following lines on the script. It may be any interpreter that you
want: bash, sh, ksh, perl, python,etc..

In .bashrc, .bash_profile or other files that are "included" from
other interpreter/scripts you dont need the interpreter line.

As a note, if you are planning to write portable scripts, please use
#!/bin/sh and avoid "bashism" (like [ $foo -ge 1 ]) in the syntax.

Greetings


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



bash scripts and files

2007-12-30 Thread michael
Folks, can somebody point me to an authorative reference that  
explains when one needs to put, eg,

 #!/bin/bash
as the first line of a script and whether or not it's required/ 
surplus/ignored for bash specific files such as .bashrc  
and .bash_profile


many thanks but couldn't find it quickly using 'oogle. M


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]