Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

execute perl script frm shell script

by Anonymous Monk
on Mar 28, 2006 at 06:40 UTC ( #539634=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am having a simple shell script in which i check which operating system i am in and then depending of that i try to execute a perl script

my shell sctipt looks like

echo $OSTYPE sac=$OSTYPE if [ $sac = linux ]||[ $sac = solaris ]; then eval 'exec perl x -S $0 ${1+"$@"}' else eval 'exec perl x -S $0 ${1+"$@"}' fi

i want to pass all my command line arguments frm shell to perl program.
i will execute my shell script with ./shellscript -rtos -file test1.
is the was i am doing is correct and will it pass all my arguments also because here i am gettign message like ...
Can't open perl script "": No such file or directory even though it is present there is same folder as shell script

20060328 Janitored by Corion: Added formatting, code tags

Replies are listed 'Best First'.
Re: execute perl script frm shell script
by Corion (Pope) on Mar 28, 2006 at 06:53 UTC

    My guess is that perl is correct about there not being a file in your directory. Possible causes for this are:

    • The current directory is not what you think it is. Print out the current directory before you start perl, maybe by doing cwd; before you launch Perl.
    • The file looks like it has the name but really it doesn't. Maybe it is called (note the lower-case "s"), or (note the letter ell instead of the digit one) or something like that.

    But without knowing the setup, it's hard to tell. In my shell scripts, I always ensure that the current working directory is set to something sane by doing:

    BASE=$(cd $(dirname $0);pwd)

    This sets the shell variable $BASE to the directory where the script resides in. All other executions are then done relative to $BASE - in your case, I would use:

    exec perl -w "$BASE/" "$@"

    to launch the Perl script - maybe your stanza is better as it will preserve quotes - on my system, I don't have to fight with arguments with embedded whitespace.

Re: execute perl script frm shell script
by graff (Chancellor) on Mar 28, 2006 at 08:47 UTC
    It looks like both "" and "" take the same command line args, so how about a "" (not numbered), instead of a shell script, in order to pick the right one:
    #!/usr/bin/perl my $script = ( $^O =~ /linux|solaris/ ) ? "" : ""; exec $script, @ARGV;
    That assumes that you've done "chmod +x" on and, and they are in your shell's PATH (e.g., if they are in your current working directory, then your PATH should include ".").

    It might be even better if the differences between Script1 and Script2 could be merged into a single perl script, with appropriate logic to do things differently depending on the value of $^O. Then you don't need a separate "wrapper" script at all.

      here the concern is not to execute the perl script. i had done same what you had sugegsted me. but problem is different platforms have different perl verions. so i check for platfom and depending on that i call the script like
      for linux / solaris
      exec /pkg/perl/5.8/bin/perl $SOURCE_ROOT/scripts/ "$@"
      for cygwin
      exec perl $SOURCE_ROOT/scripts/ "$@" and in, i will again check if perl version 5.8 or 5.6.1 is available or not.if not i will exit. the main thing to do is to use same version of perl for any user independent of what they have in their machines. in cygwin we have to call ant perl script bu writing
      perl ......
      for avoiding this i had written shell script. if there is any way to do it in prl what i m doing in shell , perl is always better.
        problem is different platforms have different perl verions

        This is not a problem that a shell script can solve.

        What you really have to do is write your Perl script so that it will run under all the perl versions you have to deal with. This, as a general rule, is not hard, if you restrict yourself to using language features that are explained in the 2nd edition of the camel book (assuming all of the perl versions are 5.something; if you have to support version 4, then it gets a bit harder.)

        There are a handful of exceptions, very special things that are hard to do with non-recent versions of Perl, but they're obscure and mostly have to do with Unicode, and you're not likely to run into them in an environment where it's taboo to upgrade perl.

        If you're having trouble getting certain parts of the Perl script to work under certain versions of perl, try posting a question to Perlmonks about that: you'll get much better answers for Perl questions around here than you will for shell script questions. Most of us don't write shell scripts any more, because we write Perl scripts instead.

        Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
        I'm not sure I understand your reply. Anything you do in a shell script can be done in a perl script (except for setting environment variables in the shell that invokes the perl script -- you can only do that if you "source" a shell script).

        If both windows and unix/linux systems are reading "" from the same disk/directory on your local network (assuming this script is already written to be OS-independent), and all you need to worry about is whether the version of perl running your script is recent enough, you can include a statement like this in the script:

        require 5.006001; # die if we're running under an older perl version
        On the other hand, if there really are different versions of the script targeted for different systems (or different versions of perl), you can always find a way to integrate these variations into a single script that runs properly in each environment, by checking the OS type and perl version, using perl's internal global variables (cf. perlvar), refactoring the code in a sensible way to simplify the branching for the different environments.

        Anyway, I really can't figure out what you meant by this:

        the main thing to do is to use same version of perl for any user independent of what they have in their machines

        What people "have in their machines" (in terms of what version of perl is installed) is going to determine what happens when they run your script, regardless of how you write it or wrap it. If their version of perl is too old, either the script will die because you put a "require" statement for a minimum perl version number, or else (if you don't have that statement) it will die or do something bad because the older perl doesn't know what to do with your script.

Re: execute perl script frm shell script
by zer (Deacon) on Mar 28, 2006 at 06:57 UTC
    is the chmod set to execute? and which shell are you using? also the files need to be in the same spot as the shell script

      is the chmod set to execute?
      doesn't need to be since running as perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://539634]
Approved by Corion
shmem imbibes ☕☕☕ to avoid more silliness

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-05-30 11:57 GMT
Find Nodes?
    Voting Booth?