help-bash
[Top][All Lists]
Advanced

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

Re: sub-process changing value available to parent process?


From: Reuti
Subject: Re: sub-process changing value available to parent process?
Date: Mon, 6 Apr 2020 09:31:58 +0200

Am 06.04.2020 um 09:17 schrieb Sylvain Viart:

> Hi Jasper,
> 
> On 05/04/2020 12:33, Jasper wrote:
>> Sylvain Viart schreef op 2020-04-05 08:55:
>>> This idea come back again. I would like to be able to modify bash
>>> variable from a child program.
>>> [...]
>>> I would like to be able to pass modified value to the shell parent
>>> without using eval.
>>> 
>>> What shared environment could I use to ensure safe and efficient
>>> communication from a sub program with it's parent script?
>> Hi,
>> 
>> to me that is: parent_var=$( whatever the child has as output )
>> 
>> Or is that not what you are after ?
> 
> No I'm not after that, you're right.
> 
> Sorry, I realized I omitted this important information: I want to assign
> multiple values which are set by the child process.
> 
> The child program is providing multiple values, not a single one, and
> the variables names are dynamic though prefixed in a namespace, or it
> can be a associative array too.

You can set multiple values in an array then:

foo=($(echo 1 2 3))

echo ${foo[2]}
3

-- Reuti


> I was more thinking  about IPC / memory mapping / shared memory / pipe /
> network, etc.
> 
> I found I can:
> 
> # somewhat like an eval
> source <(child)
> 
> or using trap :
> 
> ::::::::::::::
> RPC_communication.sh
> ::::::::::::::
> #!/usr/bin/env bash
> 
> set -euo pipefail
> 
> update_var()
> {
>     G=$(cat $TMP_VAL)
> }
> 
> init()
> {
>     TMP_VAL=$(mktemp /dev/shm/val.XXXXX)
>     G=""
> }
> 
> # trap keyboard interrupt (control-c)
> trap update_var SIGUSR1
> 
> init
> 
> ./child.sh $$ $TMP_VAL
> echo "$G"
> 
> 
> ./child.sh $$ $TMP_VAL
> echo "$G"
> ::::::::::::::
> child.sh
> ::::::::::::::
> #!/usr/bin/env bash
> 
> set -euo pipefail
> 
> PARENT=$1
> TMP_VAL=$2
> 
> echo "child $$ $RANDOM" > "$TMP_VAL"
> kill -s SIGUSR1 $PARENT
> ========================================%<====================
> 
> I'm still exploring possibilities any suggestions are welcome.
> 
> Regards,
> Sylvain.
> 
> 
> -- 
> Sylvain Viart - GNU/Linux Sysadmin/Developer/DevOps - France
> 
> 
> 




reply via email to

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