Function to conditionally set a variable read-only

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite














If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations:



#!/bin/bash
set -e
declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
declare -r SAY=_say
declare -r SUM=_sum
declare -r MV=_mv
set +e


And I source it to get the definitions, the second time because it's in development:



$ . s.bash 

$ . s.bash
bash: declare: NOTIFY: readonly variable
Exited


Normally declare -r EXISTING_VAR would neither stop the script nor remove the old, working definition of EXISTING_VAR.



But with errexit, assigning to an existing variable is understandably a failure. The easy options are to remove -r or use set +e for that part of the script.



Barring those, is it possible to write a Bash function to take the place of declare -r but not re-assign if the name already exists?



I tried:



# arg #1: var name, #2: value
set_var_once ()
# test whether the variable with the
# name stored in $1 exists
if [[ -z "$!1" ]]
then # if it doesn't, set it
declare -r $1=$2
fi



I also tried things along the lines of eval "declare -r $1=$(eval $2)", it feels like eval is required somewhere here but I'm not sure where.



All of the versions of set_var_once result in not setting the variable they should.










share|improve this question

























    up vote
    1
    down vote

    favorite














    If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations:



    #!/bin/bash
    set -e
    declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
    declare -r SAY=_say
    declare -r SUM=_sum
    declare -r MV=_mv
    set +e


    And I source it to get the definitions, the second time because it's in development:



    $ . s.bash 

    $ . s.bash
    bash: declare: NOTIFY: readonly variable
    Exited


    Normally declare -r EXISTING_VAR would neither stop the script nor remove the old, working definition of EXISTING_VAR.



    But with errexit, assigning to an existing variable is understandably a failure. The easy options are to remove -r or use set +e for that part of the script.



    Barring those, is it possible to write a Bash function to take the place of declare -r but not re-assign if the name already exists?



    I tried:



    # arg #1: var name, #2: value
    set_var_once ()
    # test whether the variable with the
    # name stored in $1 exists
    if [[ -z "$!1" ]]
    then # if it doesn't, set it
    declare -r $1=$2
    fi



    I also tried things along the lines of eval "declare -r $1=$(eval $2)", it feels like eval is required somewhere here but I'm not sure where.



    All of the versions of set_var_once result in not setting the variable they should.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite













      If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations:



      #!/bin/bash
      set -e
      declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
      declare -r SAY=_say
      declare -r SUM=_sum
      declare -r MV=_mv
      set +e


      And I source it to get the definitions, the second time because it's in development:



      $ . s.bash 

      $ . s.bash
      bash: declare: NOTIFY: readonly variable
      Exited


      Normally declare -r EXISTING_VAR would neither stop the script nor remove the old, working definition of EXISTING_VAR.



      But with errexit, assigning to an existing variable is understandably a failure. The easy options are to remove -r or use set +e for that part of the script.



      Barring those, is it possible to write a Bash function to take the place of declare -r but not re-assign if the name already exists?



      I tried:



      # arg #1: var name, #2: value
      set_var_once ()
      # test whether the variable with the
      # name stored in $1 exists
      if [[ -z "$!1" ]]
      then # if it doesn't, set it
      declare -r $1=$2
      fi



      I also tried things along the lines of eval "declare -r $1=$(eval $2)", it feels like eval is required somewhere here but I'm not sure where.



      All of the versions of set_var_once result in not setting the variable they should.










      share|improve this question















      If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations:



      #!/bin/bash
      set -e
      declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;; esac)
      declare -r SAY=_say
      declare -r SUM=_sum
      declare -r MV=_mv
      set +e


      And I source it to get the definitions, the second time because it's in development:



      $ . s.bash 

      $ . s.bash
      bash: declare: NOTIFY: readonly variable
      Exited


      Normally declare -r EXISTING_VAR would neither stop the script nor remove the old, working definition of EXISTING_VAR.



      But with errexit, assigning to an existing variable is understandably a failure. The easy options are to remove -r or use set +e for that part of the script.



      Barring those, is it possible to write a Bash function to take the place of declare -r but not re-assign if the name already exists?



      I tried:



      # arg #1: var name, #2: value
      set_var_once ()
      # test whether the variable with the
      # name stored in $1 exists
      if [[ -z "$!1" ]]
      then # if it doesn't, set it
      declare -r $1=$2
      fi



      I also tried things along the lines of eval "declare -r $1=$(eval $2)", it feels like eval is required somewhere here but I'm not sure where.



      All of the versions of set_var_once result in not setting the variable they should.







      bash shell-script variable readonly declare






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 12 mins ago









      cat

      1,66021135




      1,66021135

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478515%2ffunction-to-conditionally-set-a-variable-read-only%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478515%2ffunction-to-conditionally-set-a-variable-read-only%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?