[Top][All Lists]

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

Re: repost of alike 'missing a way to extract data'

From: Greg Wooledge
Subject: Re: repost of alike 'missing a way to extract data'
Date: Thu, 18 Mar 2021 09:30:59 -0400

On Thu, Mar 18, 2021 at 02:15:19PM +0100, Alex fxmbsw7 Ratchev wrote:
> pseudo exmaple

Why not give a REAL example?

> declare -A big=( mess of data )
> var=$( declare -p big )
> now to extract the elements, beware with mess of data i dont mean 'mess'
> 'of' 'data'

You don't write bash code to parse bash syntax.  You let bash do it
for you.

Apparently you are trying to serialize an associative array and transmit
it from one instance of bash to another.  That's totally fine, and
declare -p is the right tool for that.

In the second instance of bash, to reconstruct the serialized array,
you simply eval the serialization (or source it, if it's in a file).

In the first script:

declare -A hash=(...)
export serialized_hash="$(declare -p hash)"

Then, inside otherscript:

eval "$serialized_hash"

Now you have an associative array named "hash", with a copy of the contents
from the first script.  If you want to "extract the elements", you simply
use "${hash[key1]}" and so on.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]