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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: perl and shell
by zentara (Cardinal) on Jun 30, 2007 at 16:40 UTC
Re: perl and shell
by Corion (Patriarch) on Jun 30, 2007 at 16:42 UTC

    Whydid you first discuss this with us in the CB but then ignore and not even mention our advice or the problem you're trying to solve? Why don't you even post the code you have so far??

    I'm confident you have read system and the section in perlop about "backticks", so maybe you can tell us how the sample code there could be improved, or at least where you have problems with it.

      Monks, My code is
      #!/usr/bin/perl system('./hello.sh FileOpen.pl') == 0 or die "Couldn't launch './hell +o.sh': $! / $?";
      and output is
      perl Sample.pl Hello Madam It is Working ./hello.sh: line 3: #!/usr/bin/perl: No such file or directory Couldn't launch './hello.sh': / 32512 at Sample.pl line 2.
      The script is
      #!/bin/bash echo 'Hello Madam It is Working'; `cat $1`;
      I am new to perl with shell thats why i added in this portion
      and i have a doubt is there any module "shell" . please help me from where i have to download.
      Thanks in advance
        `cat $1`

        That doesn't make much sense. You're trying to run the contents of FileOpen.pl as a shell command...

        Just write ./$1 instead and everything will be fine. The "./" part might be required, as on Unix, "." (the current directory) is typically not in the searchpath for executables (for security reasons).

Re: perl and shell
by cdarke (Prior) on Jul 01, 2007 at 08:15 UTC
    Be careful of passing parameters with no checks. For example, a simple way might be:
    my $status = system ("fred.bash @ARGV");
    then someone calls it like this:
    $ myscript.pl 'Program Files'
    and two arguments get passed. You also have a problem if the arguments contain shell meta-characters like quotes, $, or ! in the data, which must be 'escaped' before passing. So you end up having to do something like this before calling system:
    for (@ARGV) {$_ = "\Q$_\E"}

    My point is that you should not blindly throw data from Perl to the shell. Likewise when using arguments in shell scripts always enclose them in double quotes, including: ".\$1", although even that is not bullet proof.
    You might also consider just using one language to solve your problem (Perl or Bash) rather than two. I know, sometimes we do that just to get a job done, but it is not a good long term design.
      And then there's the problem of people calling it like this:
      $ myscript.pl ' ; rm -rf ~'
      Blindly passing unchecked user input to a command is a very bad idea unless you know that the code will only ever be called by trusted users. Even then, checking is generally good to guard against user error.

      (Granted, in this simple case, the calling user can't convince it that easily to do anything that they couldn't do from the command line anyhow unless myscript.pl runs SUID or the like, but the principle remains.)