John Kirk on Fri, 14 Apr 2000 15:51:42 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

shebang pointing to script?


Hi all,

  I've hit a snag that makes me think there's something I've missed
or something I don't know.  The initial issue was that I wanted to
use shebang notation in one file to have that file, when executed,
cause another perl script (itself using shebang notation referring
to perl) to read the first file and process it.  When my initial
tries didn't work, I tried to fall back to a position where the
first file was written to look like a perl script whose first line
called exec() as in the following experiments, below.

  File "tst00" is intended to be invoked under another name, and
its only action, for these tests, is to print the contents of the
file with that other name.

  File "tst01" is such another file (that is executable, since its
name is being handed out as the one under which the "tst00" script
is being invoked, although I don't expect to execute it, in these
tests).

  Files "tst02" and "tst03" are two attempts to execute "tst00"
while telling it that it was invoked under the name, "tst01", but
it never gets the message.  These follow the suggestions made at
the end of the entry for the perl function, "exec()", in the
camel book (page 164 of my "second edition").  They differ only
in the syntax used to call the "exec()" function.

  File "tst04" is just a soft link to "tst00", and when I invoke it
from the command line, or via scripts "tst05" and "tst06", the
"tst00" program is executed, and recognizes it was invoked under
the "tst04" name.  This just verifies that "tst00" is recognizing
names other than its own as ones under which it was invoked.

  I dunno, at this point:

       (1) why I can't make "tst00" think it's being invoked under
another name from another script, as in "tst02" and "tst03".

       (2) how to make the other script get "tst00" to come back
and look at its own self (the other, calling, script).  I don't
show any experiments attempting this.

   and (3) how, preferably, to achieve (2) by means only of the
contents of the first (shebang) line in the calling script.  I can
see why this won't work just by specifying the "tst00" script in
the shebang-style notation (I suppose I could call it from a special
wrapper compiled in C, so then it would work, I think, but that
introduces additional factors I was hoping to avoid.) but I had
hoped to be able to invoke perl with an "-e <command>", or
something, in a shebang line.  So far, I can't see how to do it.

  Ultimately, it might be nice if the shell's shebang notation
could be made recursive, so that the most deeply nested of scripts
in a series, one calling another, would get the information that
it was ultimately invoked by the first -- command-line -- name
used.  No?

  Tell me, oh gurus, the error of my vision!

             regards,       -- John Kirk

file "tst00":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst00
tst00 -- as ./tst00
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

file "tst01":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w
#                     file: tst01

print "executing tst01.\n";

1;
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst01
executing tst01.
----------------------- snip, snip ----------------------------------

  I didn't expect this file to execute in any of the experiments, but
rather expected the "tst00" script to print its contents when things
worked as intended.

file "tst02":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w
exec {'tst00'} 'tst01','arg1','arg2';
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst02
tst00 -- as ./tst00
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

  It properly invokes the "tst00" script, but that script fails to
see that it should have printed out the "tst01" file instead of its
own self.

file "tst03":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w
my $real='tst00';
exec $real 'tst01','arg1','arg2';
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst03
tst00 -- as ./tst00
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

  This version of the "tst02" script above behaves just like it, as
the camel book claims -- but, also, doesn't get the message that it's
supposed to think it was invoked under the "tst01" name.

file "tst04", which is a soft link to file "tst00" executes like this:

----------------------- snip, snip ----------------------------------
prompt> ln -s tst00 tst04
prompt> ls -alF tst04
lrwxrwxrwx   1   user     user    5 Apr 14 12:34 tst04 ->tst00*
prompt> tst04
tst00 -- as ./tst04
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

At least, in this case, when "tst00" executes, it thinks it was invoked
as "tst04".  It's not much help for my purposes because "tst04"s
apparent contents are just that of the original "tst00" file.

file "tst05":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w 
exec {'tst04'} 'tst01','arg1','arg2';
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst05
tst00 -- as ./tst04
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;
----------------------- snip, snip ----------------------------------

  The soft link still has its not-very-helpful effect.

file "tst06":
----------------------- snip, snip ----------------------------------
#!/usr/bin/perl -w
my $real='tst04';
exec $real 'tst01','arg1','arg2';
----------------------- snip, snip ----------------------------------

executes like this:

----------------------- snip, snip ----------------------------------
prompt> tst06
tst00 -- as ./tst04
#!/usr/bin/perl -w
$IFH=$0;print"tst00 -- as $IFH\n";open IFH;while(<IFH>){print;};1;   
----------------------- snip, snip ----------------------------------

  And, similarly, for the other syntax for invoking exec().
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**