Advertisement
Guest User

Untitled

a guest
Mar 27th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.46 KB | None | 0 0
  1. var_store()
  2. {
  3.         trap '' INT
  4.  
  5.         while read -r command arg1 arg2 arg3
  6.         do
  7.                 case ${command} in
  8.  
  9.                         WRITE)
  10.                                 declare ${arg1}=${arg2}
  11.                                 printf "${arg1}=${arg2}\n" > ${arg3}
  12.                                 ;;
  13.                         READ)
  14.                                 if [[ -v "${arg1}" ]]
  15.                                 then
  16.                                         printf "${!arg1}\n" > ${arg2}
  17.                                 else
  18.                                         printf "unset\n" > ${arg2}
  19.                                 fi
  20.                                 ;;
  21.                         *)
  22.                                 printf '%s\n' "Unknown command: ${command}" >&2
  23.                                 ;;
  24.  
  25.                 esac
  26.  
  27.         done < ${var_store_input_fifo}
  28. }
  29.  
  30. var_store_write()
  31. {
  32.         local var_name=${1}
  33.         local var_value=${2}
  34.  
  35.         echo "writing $var_store_input_fifo"
  36.  
  37.         printf "WRITE ${var_name} ${var_value} ${var_store_output_fifo}\n" > ${var_store_input_fifo}
  38.  
  39.         echo "written"
  40.  
  41.         read -r write_confirmation < ${var_store_output_fifo}
  42.  
  43.         echo $write_confirmation
  44.  
  45.         if [[ "${write_confirmation}" != "${var_name}=${var_value}" ]]
  46.         then
  47.                 printf '%s\n' "var_store write error: var_name=${var_name}; var_value=${var_value}; write_confirmation=${write_confirmation}" >&2
  48.         fi
  49. }
  50.  
  51. var_store_read()
  52. {
  53.         local -n var_name=${1}
  54.         local -n var_dest=${2:-${1}}
  55.         local exit_on_unset=${3:-1}
  56.  
  57.         printf "READ ${!var_name} ${var_store_output_fifo}\n" > ${var_store_input_fifo}
  58.  
  59.         read -r var_dest < ${var_store_output_fifo}
  60.  
  61.         if [[ "${var_dest}" == "unset" ]]
  62.         then
  63.                 if ((exit_on_unset))
  64.                 then
  65.                         printf '%s\n' "${!var_name} unset and exit_on_unset enabled." >&2
  66.                         kill -$$ INT
  67.                 else
  68.                         return 1
  69.                 fi
  70.         fi
  71. }
  72.  
  73. var_store_link()
  74. {
  75.         local path=${1}
  76.         local name=${2}
  77.  
  78.         var_store_output_fifo="${path}/${name}_var_store_output_fifo"
  79.         [[ -p ${var_store_output_fifo} ]] && printf '%s\n' "${name} already linked" >&2
  80.         mkfifo ${var_store_output_fifo}
  81. }
  82.  
  83. var_store_unlink()
  84. {
  85.         [[ -p ${var_store_output_fifo} ]] && rm ${var_store_output_fifo}
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement