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: Sylvain Viart
Subject: Re: sub-process changing value available to parent process?
Date: Mon, 6 Apr 2020 09:17:58 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0

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.

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]