Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Is this possible in ZSH?



On Nov 14,  3:12pm, nix@xxxxxxxxxxxxxxxx wrote:
}
} Is it possible to append background process results to a variable or
} to an array without waiting for that job to finish?

You're effectively asking whether the shell implements multiprocess
shared memory, because otherwise "process results" are only available
as either pipe I/O or as the exit status of the job.  The short answer
is no, you can't declare a shell variable that shares writable memory
with a forked child.

There are several alternatives to simulate the effect you want.  You
can use either coprocesses (several examples are in the mailing list
archives, search for "coproc") or the zsh/net/socket module to set up
multiple simultaneous connections from background jobs to the shell.
You can use a trap on SIGCHLD or a loop on zselect (the zsh/zselect
module) to pick up the data as the jobs exit.

Or you can use the zsh/mapfile module to simulate a shared variable.
Compare:

x=32
repeat 3 do (let "x /= 2"); print $x; done
unset x

zmodload zsh/mapfile
mapfile[shared]=32
repeat 3 do (let "mapfile[shared] /= 2"); print $mapfile[shared]; done
unset 'mapfile[shared]'

Just be sure the name you use in place of "shared" is something unique,
because you will destroy the contents of any file in $PWD that has that
name.



Messages sorted by: Reverse Date, Date, Thread, Author