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

Passing values from Perl script to shell script

by sen (Hermit)
on Dec 09, 2009 at 15:08 UTC ( #811938=perlquestion: print w/ replies, xml ) Need Help??
sen has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Not able to pass the scalar value from Perl script to shell script.

#Perl script $var1 = 3; $var2 = 5; system ("shell_script.sh", $var1, $var2);

Any help much appreciated.

Comment on Passing values from Perl script to shell script
Download Code
Re: Passing values from Perl script to shell script
by moritz (Cardinal) on Dec 09, 2009 at 15:18 UTC
    You don't put any values in $var1 or $var2, so it's not surprising that the shell script receives none.
Re: Passing values from Perl script to shell script
by gmargo (Hermit) on Dec 09, 2009 at 15:59 UTC

    The following works for me. Perl code:

    #!/usr/bin/perl -w use strict; use warnings; use POSIX ":sys_wait_h"; my $var1 = 3; my $var2 = 5; system ("./perl651.sh", $var1, $var2); my $status = $?; if ($status == -1) { print "failed to execute: $!\n"; } elsif (WIFEXITED($status)) { print "exited with return code ".WEXITSTATUS($status)."\n"; } elsif (WIFSIGNALED($status)) { print "terminated by signal ".WTERMSIG($status)."\n"; } else { print "other status $status\n"; }

    Shell code (filename = perl651.sh)

    #!/bin/sh if [ $# -ne 2 ] then echo "Expecting 2 arguments, only $# provided" exit 1 fi # return code is sum of two arguments RC=`expr $1 + $2` echo "Sum of $1 + $2 is $RC" exit $RC;

    Results:

    Sum of 3 + 5 is 8 exited with return code 8
Re: Passing values from Perl script to shell script
by leocharre (Priest) on Dec 09, 2009 at 18:24 UTC
    May be of interest; I came across String::ShellQuote, very nice, escapes anything..
    use String::ShellQuote 'shell_quote'; my $cmd = sprintf 'cp %s %s', shell_quote($weirdpath), shell_quote($weird2); `$cmd`;
      May be of interest; I came across String::ShellQuote, very nice, escapes anything..

      Nope, it doesn't quote "anything". It works just for some unnamed version of some unnamed bourne shell. The author wanted to add more shells, but he did not since 2005. The test.t look very strange, especially I don't see any reasonable test for passing arguments via a shell.

      So, it's old, unmaintained, not well-tested, and broken for all shells except for that unspecified bourne shell. Use the multi-argument forms of exec, system, or open instead, they do not need quoting.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Well, if I want to take a text string, and turn it into a string that posix command line will interpret as one argument- what do you suggest to use instead? (And what you suggested in the post is not a replacement for String::ShellQuote)

        If you do not have a suggestion- I must say- String::ShellQuote has been working well for me. That said- as per your observations- maybe it's time to contact the author and offer contributions?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://811938]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-10 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls