return two values from perl to bash variables

折月煮酒 提交于 2019-12-11 19:22: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


来源:https://stackoverflow.com/questions/17031237/return-two-values-from-perl-to-bash-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!