I'm working on a new way to share variables between processes:
#!/bin/bash
var_store()
{
while read -r -u "${var_store_input_fd}" command arg1 arg2
do
echo $command $arg1 $arg2
case ${command} in
WRITE)
declare ${arg1}=${arg2}
;;
READ)
printf "${!arg1}\n" > ${arg2}
;;
esac
done
}
var_store_write()
{
local var=${1}
local val=${2}
printf "WRITE ${var} ${val}\n" >&"${var_store_input_fd}"
}
var_store_read()
{
local -n var=${1}
printf "READ ${!var} var_store_output_fifo\n" >&"${var_store_input_fd}"
read -r var < var_store_output_fifo
}
var_store_link()
{
mkfifo var_store_output_fifo
exec <> var_store_output_fifo
}
var_store_unlink()
{
[[ -p var_store_output_fifo ]] && rm var_store_output_fifo
}
process_1()
{
var_store_link
var_store_write x 100
var_store_read x
echo ${x}
var_store_unlink
}
exec {var_store_input_fd}<> <(:) || true
var_store &
process_1
This should be faster than writing out to temporary files and reading/rereading from those temporary files.
Any thoughts on how I might improve?