Fellow Monks,

We have a huge number of installed scripts, that all are written with #!/<perlpath>/bin/perl in line 1 of the scripts (we're in a Solaris environment, and have had an exclusive Sparc compute environment). We've recently added gobs of i386/Solaris servers in our server room, and I want to be able to have users run all these hundreds of scripts on the i386, or sparc platforms, without having to change all the scripts, and/or make separate i386 versions that point to a separate i386 perl build.

So, I've built the sparc, and the i386 versions *together*, and installed the binaries separately using --installbin=<perlpath>/i386/bin (for i386) and --installbin=<perlpath>/sparc/bin for sparc. Then in the directory "<perlpath>/bin", I made the file "perl" into a shell script, to figure out what machinetype the user is on, then exec to the ../i386/bin/perl, or ../sparc/bin/perl, respectively. So my single installation, has a separate architecture area for sun4-solaris-64int, and another for i86pc-solaris-64int. The non-architecture-dependent modules are all shared, and there are separate bin dirs for the perl binaries. The "perl-5.8.8/bin" directory then is just filled with wrappers that resolve the correct build bin...

So, this works fine, if the tool/script is written as follows:

#!/bin/sh -- # -*- perl -*- eval 'exec <perlpath>/bin/perl -S $0 ${1+"$@"}' if 0;
because the unix kernel is just doing a sh exec to the <perlpath>/bin/perl file, which is my bourne shell wrapper. No problem. The problem is that all the tools/scripts use the form:
#!<perlpath>/bin/perl
and in this case, the unix kernel is looking at <perlpath>/bin/perl, and noting that this is not a binary file, and that it doesn't return the appropriate "magic number", and so decides, that this file is not execl()-able. Therefore, it wrongly decides that it's a csh script (because it's char#1,col#1 character is "#"), and therefore begins parsing the script as a csh script, and quite quickly royally croaks....

OK, so (finally my question!) how can I trick the kernel into executing the target interpreter path of a perl script that begins with #!<perlpath>/bin/perl, when <perlpath>/bin/perl is a bourne shell wrapper? Here's the wrapper script, if you're interested:

#!/bin/sh ## this is the wrapper file: <toolroot>/perl-5.8.8/bin/perl die () { echo "ERROR: $1"; exit 1; } tool=`/usr/bin/basename $0` perlroot=<mytoolroot>/tools/perl-5.8.8 arch=`/usr/bin/mach`; ## try to exec the perl binary exe=$perlroot/$arch/bin/$tool if [ -x $exe ]; then eval 'exec $exe ${1+"$@"}' else die "Couldn't find $tool executable: \"$exe\"."; fi

In reply to using "#!/<perlpath>/bin/perl" to exec a shell script? by cadphile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.