We help IT Professionals succeed at work.

/usr/bin/perl wrapper for multiple OS

hiatt
hiatt asked
on
551 Views
Last Modified: 2008-02-26
Hi,

 I wrote a really brain damaged wrapper(see below) to select between
the sunOS or solaris perl binary. This works fine when invoked from
the command line, however, for scripts that actually invoke the
wrapper (ie, #!/bin/perl) it fails horrible. In fact, it tries to
execute the script without invoking perl.

 Could someone give me a clue as to what I am doing wrong (besides using
/bin/sh). :)

 Thank you,

don
 


---- snip (/bin/perl) ---

#!/bin/sh
sunPERL="/usr/test/perl/sparc-sun-sunos4.1.3/bin/perl"
solarisPERL="/usr/test/perl/sparc-sun-solaris2.5/bin/perl"
PERL=${sunPERL} # default

if [ -f /bin/uname ] ; then  # Make sure we have uname
        VERSION=`/bin/uname -r`
        #echo "OS VERSION: ${VERSION}"
        if [ $VERSION -gt 4 ] ; then  # Solaris
                PERL=${solarisPERL}
        else                          # SunOS
                PERL=${sunPERL}
        fi
else
        echo "Error: Can not find uname"
        exit 1
fi

exec ${PERL} ${1+"$@"}

exit 1

--
"Don Hiatt" <hiatt@cig.mot.com>
Key fingerprint =  72 83 9B 82 FF DB F5 97  53 78 01 00 22 4C 95 A0
Comment
Watch Question

Commented:
Hi,

   I'm not sure where the problem is. But I've a simple solution
to your problem by using symlink.

----- Your perl script -----
#!/usr/myperl
perl code ....

-----

---- Your shell script ----
#!/bin/sh
....
if solaris ...
   ln -fs /usr/bin/solaris/perl /usr/myperl
else ln -fs /usr/bin/sunos/perl /usr/myperl
fi
/usr/home/my/perl/script
--------------------------------------------
By using this strategy .. you will only be creating a symbolic link to the actual perl binary of your choice. So your scripts will use the same path and you don't have to "exec perl ..."
in your shell script which is causing the problem.

Regards,
Minh Lai


Author

Commented:
Minh Lai,

 I know I can do something like this. The problem is that I would have to modify *a lot* of scripts which are spread out across *many* hosts. So, this really isn't possible. What I need is a a way to have each script (with #!/bin/perl) invoke /bin/perl which is actually a wrapper which determines your arch/os (dec, next, sun, solaris, aix, sgi, etc) and then invoke the "real" pearl (ie, pearl binary for that architecture/os).

  Thank you very much for your suggestion!

don

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
This seems to be more of a Unix problem than a Perl problem
I don't think you can (portably) #!pathname to another #!pathname
Couldn't you make /bin/perl a link on each local host
pointing to the appropriate binary for that host?
(after all you're depending on /bin/sh and /bin/uname to point
to the binary appropriate to each host)
 

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
This won't work either. It seems it is not possible using a #!
script. Have a look at this:

http://xp10.dejanews.com/getdoc.xp?recnum=5434053&search=thread&threaded=1&server=db95q4&CONTEXT=873305307.962267844&HIT_CONTEXT=873304654.1167722027&HIT_NUM=27&hitnum=3&NTL=1

Thank you for your help,

don

I've read the article you mentioned.
I think it doesn't match our perl problem.
Reason:
  exec, in fact, starts an interpreter, but this one (perl) does
  not depend on the #! (nevertheless, AFAIK perl checks this line
  for its own options)
This would be true if the started program (by exec) is an script to be interpreted by a shell itself. But we start an an other program, perl.

Author

Commented:
Yes, but the problem is that I have some script called "foo".
And "foo" starts like this: "#!/usr/bin/perl". Also note, that
"foo" requires arguments from the user. Now, "/usr/bin/perl" is
a shell script that exec the real perl. I have tried the "-S.."
options, and what happens is that the script, "foo", is executed
without the perl interpreter. Example,
      
      Say, "foo" looks like this

      #!/usr/test/bin/perl
      print "hi";
      exit;

         When I execute the wrapper, I get

            print: Command not found.


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.