(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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.