Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var_store()
- {
- trap '' INT
- while read -r command arg1 arg2 arg3
- do
- case ${command} in
- WRITE)
- declare ${arg1}=${arg2}
- printf "${arg1}=${arg2}\n" > ${arg3}
- ;;
- READ)
- if [[ -v "${arg1}" ]]
- then
- printf "${!arg1}\n" > ${arg2}
- else
- printf "unset\n" > ${arg2}
- fi
- ;;
- *)
- printf '%s\n' "Unknown command: ${command}" >&2
- ;;
- esac
- done < ${var_store_input_fifo}
- }
- var_store_write()
- {
- local var_name=${1}
- local var_value=${2}
- echo "writing $var_store_input_fifo"
- printf "WRITE ${var_name} ${var_value} ${var_store_output_fifo}\n" > ${var_store_input_fifo}
- echo "written"
- read -r write_confirmation < ${var_store_output_fifo}
- echo $write_confirmation
- if [[ "${write_confirmation}" != "${var_name}=${var_value}" ]]
- then
- printf '%s\n' "var_store write error: var_name=${var_name}; var_value=${var_value}; write_confirmation=${write_confirmation}" >&2
- fi
- }
- var_store_read()
- {
- local -n var_name=${1}
- local -n var_dest=${2:-${1}}
- local exit_on_unset=${3:-1}
- printf "READ ${!var_name} ${var_store_output_fifo}\n" > ${var_store_input_fifo}
- read -r var_dest < ${var_store_output_fifo}
- if [[ "${var_dest}" == "unset" ]]
- then
- if ((exit_on_unset))
- then
- printf '%s\n' "${!var_name} unset and exit_on_unset enabled." >&2
- kill -$$ INT
- else
- return 1
- fi
- fi
- }
- var_store_link()
- {
- local path=${1}
- local name=${2}
- var_store_output_fifo="${path}/${name}_var_store_output_fifo"
- [[ -p ${var_store_output_fifo} ]] && printf '%s\n' "${name} already linked" >&2
- mkfifo ${var_store_output_fifo}
- }
- var_store_unlink()
- {
- [[ -p ${var_store_output_fifo} ]] && rm ${var_store_output_fifo}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement