[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: exporting a variable to global shells



cga2000 wrote:
On Fri, May 11, 2007 at 12:33:12PM EDT, Bob McGowan wrote:

<snip>

  3.  in the parent script, where you use your script, change it to be:

      HTTP_proxy=$(getproxyip)

.. you can take it one tiny step further by using an array:

. child:

cz=($c0 $c1 $c2)    /* .. $c4 .. etc.                            */
echo "${cz[@]}"

. parent:

cz=($(child))       /* note added outer parentheses ..          */

$p0="${cz[0]}"
$p1="${cz[1]}"
$p2="${cz[1]}"

..

I think you need a fairly recent version of bash to do this .. Dunno
about other shells.

Thanks,
cga



Actually, unless you need an array, this is overkill, if the objective is to get a series of single values in a series of simple variables (p0 ... p?).

Assuming these assignments:  x=a y=b z=c p=d

  echo "${cz[@]}" # where cz is (a b c d), from cz=($x $y $z $p)

and

  echo $x $y $z $p

generate the same results.

And you can read data into a series of variables using the 'read' built in command:

  read x y z p

The only issue being, if you don't have total control on the output of the command being read, and it generates more than 4 fields, the 4th to last field all get stuffed into p, so in that case you'd want to do:

  read x y z p rest

So:

  $ echo a b c d | read x y z p
  $ echo $x $y $z $p
  a b c d

And read has been part of Bourne shells for ages, so there's less of a backward compatibility problem.

Bob

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Reply to: