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

Hi all,

I'll try to be as clear as i can - sorry if i'm not since english isn't my first language.

I'm working on a huge bash script, that needs to call perl scripts to do some actions.

Basically, all the local job is done by a bash parent, who calls child perl scrits for all the remote actions.

For example, i call a perl script with parameters/options who ask a remote server for informations and store them into an array.

What i'd like to do now is to be able to access this array in my parent bash.

What's the good way to do this?

Thanks

  • Comment on perl child to return var to a bash parent

Replies are listed 'Best First'.
Re: perl child to return var to a bash parent
by shmem (Chancellor) on May 02, 2011 at 13:17 UTC

    For starters:

    perl script which outputs an array:

    #!/usr/bin/perl # line 2 array.pl @ary = $ARGV[0] eq '-n'? (10..15) : (a..f); print "@ary\n";
    perl script which outputs a hash:
    #!/usr/bin/perl # line 2 script hash.pl my %hash = ( foo => 42, bar => 1e3, quux => 0, ); print "$_ $hash{$_}\n" for sort keys %hash;
    bash script using them:
    #!/bin/bash # script example.sh declare -a LETTERS declare -a NUMBERS NUMBERS=(`array.pl -n`) LETTERS=($(array.pl)) i=0 while [ $i -lt ${#NUMBERS[@]} ]; do echo ${NUMBERS[$i]} is ${LETTERS[$i]} i=$((i + 1)) done ./hash.pl | while read key value do echo string: $key, number: $value done
    bash$ ./example.sh 10 is a 11 is b 12 is c 13 is d 14 is e 15 is f string: bar, number: 1000 string: foo, number: 42 string: quux, number: 0
Re: perl child to return var to a bash parent
by Anonymous Monk on May 02, 2011 at 11:15 UTC
    Read perlipc, basically, the only way parent/child process can communicate is through stdin/sdtout/stderr, so your bash program would read whatever your perl program prints to stdout, and do something with it

    Since you already have perl, forget about writing bash, just write perl :)

      Thanks for your reply :)

      Yes next time i'll go with perl but i spent so many days on the bash prog that it's too late for this one :(

      I was trying to avoid writing to a temp file, but finally it seems to be the less worst solution...

        I was trying to avoid writing to a temp file, but finally it seems to be the less worst solution...
        Why? Just call from your bash script:
        eval result=([1]=$(perl -e 'print join " ",map rand 10,1..50'))
        and then use the results:
        echo ${result[1]},${result[50]}
        No temp files needed.
Re: perl child to return var to a bash parent
by anonymized user 468275 (Curate) on May 02, 2011 at 11:59 UTC
    The anonymous monk is right. I tend to output columns tab- separated including a column header row. In general there will be 0, 1 or more data rows in such situations. A bash script can read this in directly although given that the perl scripts are new, so must the interfacing be; so I would be inclined to do as much as possible in Perl and avoid new functionality in the bash as much as possible, although the implication is that dispensing with bash completely is not likely to be the preferred option.

    One world, one people