Spooky has asked for the wisdom of the Perl Monks concerning the following question:

Within a Perl program I want to build a unix script, pass arguments into it, and execute it within Perl. What's the best way to do this?
  • Comment on building and running a unix script with Perl

Replies are listed 'Best First'.
Re: building and running a unix script with Perl
by cdarke (Prior) on Jan 25, 2010 at 14:59 UTC
    It is quite easy to write files from Perl, see open and print. Be sure to chmod (assuming UNIX). When calling the script using system or qx it is probably best to pass the full path name.

    On the other hand, would it not be better to write it in Perl? Off hand I can't think of anything shell scripts can do (even the very latest versions) that Perl cannot.
      There's nothing shell scripts can do that cannot be done in Perl1. But there are things that are easier done in a shell script than in Perl. Sourcing other shell scripts for instance (and hence, getting access to environment variables). You can't really beat
      . /path/to/config/file
      in Perl for ease, neither keystrokes.

      1Of course, there's nothing a Perl program can do that cannot be done in Python, Java or C. That doesn't imply some people rather write Perl...

Re: building and running a unix script with Perl
by tirwhan (Abbot) on Jan 25, 2010 at 15:27 UTC

    Here's one way

    perl -e 'system(qq(bash -c "echo I am a shell script"));'

    That's rather nonsensical of course, but without further details on what you want to do, and why you would want to do it this way, I can't be much more helpful.


    All dogma is stupid.
Re: building and running a unix script with Perl
by eff_i_g (Curate) on Jan 25, 2010 at 17:03 UTC

      No, it isn't, because it quotes the wrong way for (almost) every shell. Don't use it. See Re^2: Passing values from Perl script to shell script.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)