Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Convert Shell script to Perl

by akrrs7 (Acolyte)
on Oct 10, 2011 at 13:22 UTC ( #930628=perlquestion: print w/replies, xml ) Need Help??
akrrs7 has asked for the wisdom of the Perl Monks concerning the following question:

This is the shell script

#!/usr/bin/sh export LIBDIR=$MY_XALANHOME/bin export LOCALCLASSPATH=${LIBDIR}/xalan.jar export LOCALCLASSPATH=${LOCALCLASSPATH}:${LIBDIR}/xml-apis.jar export LOCALCLASSPATH=${LOCALCLASSPATH}:${LIBDIR}/xercesImpl.jar JAVACMD=$JAVA_HOME/bin/java $JAVACMD -cp $LOCALCLASSPATH org.apache.xalan.xslt.Process "$@"

This is the perl equivalent script

#!/usr/bin/perl use strict; use warnings; $ENV {'LIBDIR'}='$MY_XALANHOME/bin'; $ENV {'LOCALCLASSPATH'}='$LIBDIR/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$LOCALCLASSPATH:$LIBDIR/xml-apis.jar'; $ENV {'LOCALCLASSPATH'}='$LOCALCLASSPATH:$LIBDIR/xercesImpl.jar'; JAVACMD=$JAVA_HOME/bin/java; $JAVACMD -cp $LOCALCLASSPATH org.apache.xalan.xslt.Process "$@";

Please review and advise where I could be off on the perl script. I need this to work in Windows as well as Unix.


Replies are listed 'Best First'.
Re: Convert Shell script to Perl
by Corion (Pope) on Oct 10, 2011 at 13:28 UTC

    What you posted is not valid Perl. Running what you think your Perl script is will make Perl tell you so. Please correct your script. Maybe you want to read system, but before that, also do read perlintro and perlsyn.

    Perl is not a shell or shell-alike language.

      Hello... I am really new at you've surmised. I have read the perlintro document but am still confused on how to set environment variables. Thanks...

        You are confused about so many things that it does not make sense to give you small pointers. You need to take a step back and start learning Perl. See the books section of, and maybe the Modern Perl book by chromatic, if you're in need of a free book and already know how to program in some other programming language.

Re: Convert Shell script to Perl
by Utilitarian (Vicar) on Oct 10, 2011 at 14:02 UTC
    You could set the environment variables and run exec to call the script
    [skrynesaver@busybox ~]$ cat ~/tmp/ #!/usr/bin/perl $ENV{'THE_VAR'}= "This has been set in the environment of the script"; exec "$ENV{HOME}/tmp/" [skrynesaver@busybox ~]$ cat ~/tmp/ #!/bin/bash echo $THE_VAR [skrynesaver@busybox ~]$ perl tmp/ This has been set in the environment of the script
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      exec does not behave in the same way on Windows and UNIX, so it might be better to use system instead, which is similar on both (although there are still a few differences).

      Although the latest ksh93 versions exec on the last statement within a script, I don't think that bash does.

      One other thing to be aware of, environment variables on UNIX are case sensitive but on Windows are not - that can catch you out.
        This is the code:
        use strict; use warnings; $ENV {'LIBDIR'}='$MY_XALANHOME/bin'; $ENV {'LOCALCLASSPATH'}='$ENV{LIBDIR}:/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xml-apis.ja +r'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xercesImpl. +jar';
        When I run the perl program from the command line using - perl - I don't get any error messages. When I run the following command from the commandline:
        perl $ENV{'LIBDIR'}='C:\SOMEFOLDER';
        I get a message saying - can't open perl script "$ENV": no such file or directory. I would've expected to create a new environment variable called LIBDIR with the c:\somefolder as the value ? Thanks
      I guess..I know that $ENV is similar to a hashmap and has a key and a value...
      $ENV {'LIBDIR'}='$MY_XALANHOME/bin';
      seems to be the correct way to set that. Please confirm. What I am not sure is - should I also set the JAVACMD as an environment variable using $ENV ? Thanks...
      I've tried the following code:
      use strict; use warnings; $ENV {'LIBDIR'}='C:/eWorkspace/myPerlProgram'; $ENV {'LOCALCLASSPATH'}='$ENV{LIBDIR}:/xalan.jar'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xml-apis.ja +r'; $ENV {'LOCALCLASSPATH'}='$ENV{LOCALCLASSPATH}:$ENV{LIBDIR}/xercesImpl. +jar'; system "$ENV{LIBDIR}/"
      I've then run this program from the command line to get the following message: Can't spawn "cmd.exe": No such file or directory at C:\eWorkspace\myPerlProgram\ line 33.
        Sorry for necroposting, but in Perl, the running code should be:

        my $JAVACMD = "$JAVA_HOME/bin/java"; my @OUTPUT = `$JAVACMD -cp "$LOCALCLASSPATH" org.apache.xalan.xslt.Pro +cess "@ARGV"`;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930628]
Approved by keszler
talexb ponders

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2017-05-26 20:20 GMT
Find Nodes?
    Voting Booth?