Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

values from perl to sh script

by wpeterma (Initiate)
on Jul 14, 2005 at 17:34 UTC ( [id://474980]=perlquestion: print w/replies, xml ) Need Help??

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

My perl program is triggered by (is the child of) a shell script. What is the best/easiest way to pass scalar values back to the script?

Replies are listed 'Best First'.
Re: values from perl to sh script
by pbeckingham (Parson) on Jul 14, 2005 at 17:41 UTC

    I would say print them to STDOUT, and capture them in the shell script. Alternatively use a file.



    pbeckingham - typist, perishable vertebrate.
      Once perl has written the values to STDOUT, what do I use to read them in to the script? Is it read? (Sorry for being a rank amateur)
        Capture them into a variable, something like
        RESULT=`perl runme.pl`
        Then your result will be in $RESULT

        Updated: Those are backticks surrounding the perl script

        Don
        WHITEPAGES.COM | INC
        Everything I've learned in life can be summed up in a small perl script!
        (Some of the following notes surely apply to bash, don't know about other shells).

        You can use the backtick operator or its equivalent (and prefearable, IMHO):

        return1=`/path/to/script.pl arg1 arg2` return2=$(/path/to/script.pl arg1 arg2)
        The latter has the added advantage of allowing nested subshell invocations.

        If your return value can have newlines, I'd suggest to use double quotes:

        return_newlines="$(/path/to/script.pl arg1 arg2)"
        And yes, you can use read as well, but you have to be extremely careful about what you want to do. Suppose you have the following script:
        #!/usr/bin/perl print $_, $/ for @ARGV;
        and you want to set the variable value to the contents of the last line:
        #!/bin/bash value=0 echo "value starts from $value" ./script.pl arg1 arg2 | while read line ; do value=$line echo "value is $value" done echo "after first while, value is $value" while read line ; do value=$line echo "value is $value" done <<<"$(./script.pl arg1 arg2)" echo "finally, value is $value"
        you obtain:
        value starts from 0 value is arg1 value is arg2 after first while, value is 0 value is arg1 value is arg2 finally, value is arg2
        Note that after the first while the variable value has not been modified. This is due to the fact that the first while cycle is being executed inside a subshell.

        OTOH, the second while is executed directly by the "current" shell. This may lead to problems if you pass a lot of data back, anyway.

        Note that there are also other ways, but there is too little space on the side of this post to write them.

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.
        Nevermind, I have it. I an exporting my variables to env, then the script has them. e.g echo ${ENVVAR} Thanks for your consideration.
Re: values from perl to sh script
by polettix (Vicar) on Jul 14, 2005 at 17:45 UTC
    The easiest is probably printing to standard output - and it's probably the best as well.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: values from perl to sh script
by northwind (Hermit) on Jul 14, 2005 at 17:50 UTC

    Easiest would be to have the perl program exit with a value; not very useful though.  You could equally print what you wanted passed and capture the data within your shell script.  Finally, you could init some system vars in your shell script and have the perl script pass data by setting them by using the %ENV hash.  The vars must be initialized in the shell script or, when the perl script exits, they will go out of scope (at least this is the behavior I have seen on my system).

Re: values from perl to sh script
by suaveant (Parson) on Jul 14, 2005 at 18:14 UTC
    I have done eval `foo.pl` in the past, then you have perl print out key value pairs.

    foo.pl #!perl %foo = (a => 1, b => 2); for(keys %foo) { print "$_=$foo{$_}\n"; }
    Not really tested, but you get the idea... echo $b in the script will echo 2, very handy

                    - Ant
                    - Some of my best work - (1 2 3)

Re: values from perl to sh script
by socketdave (Curate) on Jul 14, 2005 at 20:03 UTC
    Is it possible to replace the functionality of the shell script and the child perl script with a single piece of perl? Unless the shell script is huge or can't be replaced, I'd probably take this route.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found