return two values from perl to bash variables

2019-08-28 03:02发布

问题:

I am calling a perl script to calculate size and variation in bash script. Is there a way to return those two values to separated variables in bash, say $SIZE and $VAR. Only know how to return one value.

回答1:

Instead of returning (which is mainly used for an error/success code from the script), you can print your variables from the perl script, separated by, say, space, and then read them from bash:

#!/usr/bin/perl
$size=1;
$var=2;
print "$size $var\n";

and:

#!/bin/bash
read SIZE VAR <<<$(my_perl_script)
echo size: $SIZE var: $VAR


回答2:

you can only do that by evaluating perl's output e.g.:

from perl:

print "SIZE=1 VAR=blah"

then in shell script:

export `your perl script.pl`


回答3:

set -- $(perl yourscript)

size=$1
var=$2