Shell script to assign variable to an environment variable specified in command line argument?

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











up vote
1
down vote

favorite












I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:



# assigns first argument to environmentVariable variable
environmentVariable=$1
# assigns second argument to new_path variable
newPath=$2
# calls function locate_field to check if new_path exists in environmentVariable
locate_field $environmentVariable $newPath
# gets return value from locate_field
returnCode=$?
# environment variable is not empty
if [[ "$returnCode" -eq 0 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
# environment variable is empty
elif [[ "$returnCode" -eq 2 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$newPath
else
echo "$newPath already exists in the environment variable."
fi


How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.



function locate_field()
#parameters passed to the function
envVariable=$(eval echo '$'$1)
pathToBeAdded=$2
#flag variable found
found=0
#when given environment variable value is empty
list_length=$#envVariable
if [[ "$list_length" -eq 0 ]]; then
found=2
fi
old_value=IFS
IFS=:
for each_path in $envVariable
do
if [[ "$each_path" == "$pathToBeAdded" ]]; then
found=1
fi
done
IFS=$old_value
echo $found
return $found









share







New contributor




user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:



    # assigns first argument to environmentVariable variable
    environmentVariable=$1
    # assigns second argument to new_path variable
    newPath=$2
    # calls function locate_field to check if new_path exists in environmentVariable
    locate_field $environmentVariable $newPath
    # gets return value from locate_field
    returnCode=$?
    # environment variable is not empty
    if [[ "$returnCode" -eq 0 ]]; then
    #adds new path and exporting the environment variable
    export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
    # environment variable is empty
    elif [[ "$returnCode" -eq 2 ]]; then
    #adds new path and exporting the environment variable
    export $environmentVariable=$newPath
    else
    echo "$newPath already exists in the environment variable."
    fi


    How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.



    function locate_field()
    #parameters passed to the function
    envVariable=$(eval echo '$'$1)
    pathToBeAdded=$2
    #flag variable found
    found=0
    #when given environment variable value is empty
    list_length=$#envVariable
    if [[ "$list_length" -eq 0 ]]; then
    found=2
    fi
    old_value=IFS
    IFS=:
    for each_path in $envVariable
    do
    if [[ "$each_path" == "$pathToBeAdded" ]]; then
    found=1
    fi
    done
    IFS=$old_value
    echo $found
    return $found









    share







    New contributor




    user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:



      # assigns first argument to environmentVariable variable
      environmentVariable=$1
      # assigns second argument to new_path variable
      newPath=$2
      # calls function locate_field to check if new_path exists in environmentVariable
      locate_field $environmentVariable $newPath
      # gets return value from locate_field
      returnCode=$?
      # environment variable is not empty
      if [[ "$returnCode" -eq 0 ]]; then
      #adds new path and exporting the environment variable
      export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
      # environment variable is empty
      elif [[ "$returnCode" -eq 2 ]]; then
      #adds new path and exporting the environment variable
      export $environmentVariable=$newPath
      else
      echo "$newPath already exists in the environment variable."
      fi


      How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.



      function locate_field()
      #parameters passed to the function
      envVariable=$(eval echo '$'$1)
      pathToBeAdded=$2
      #flag variable found
      found=0
      #when given environment variable value is empty
      list_length=$#envVariable
      if [[ "$list_length" -eq 0 ]]; then
      found=2
      fi
      old_value=IFS
      IFS=:
      for each_path in $envVariable
      do
      if [[ "$each_path" == "$pathToBeAdded" ]]; then
      found=1
      fi
      done
      IFS=$old_value
      echo $found
      return $found









      share







      New contributor




      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:



      # assigns first argument to environmentVariable variable
      environmentVariable=$1
      # assigns second argument to new_path variable
      newPath=$2
      # calls function locate_field to check if new_path exists in environmentVariable
      locate_field $environmentVariable $newPath
      # gets return value from locate_field
      returnCode=$?
      # environment variable is not empty
      if [[ "$returnCode" -eq 0 ]]; then
      #adds new path and exporting the environment variable
      export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
      # environment variable is empty
      elif [[ "$returnCode" -eq 2 ]]; then
      #adds new path and exporting the environment variable
      export $environmentVariable=$newPath
      else
      echo "$newPath already exists in the environment variable."
      fi


      How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.



      function locate_field()
      #parameters passed to the function
      envVariable=$(eval echo '$'$1)
      pathToBeAdded=$2
      #flag variable found
      found=0
      #when given environment variable value is empty
      list_length=$#envVariable
      if [[ "$list_length" -eq 0 ]]; then
      found=2
      fi
      old_value=IFS
      IFS=:
      for each_path in $envVariable
      do
      if [[ "$each_path" == "$pathToBeAdded" ]]; then
      found=1
      fi
      done
      IFS=$old_value
      echo $found
      return $found







      linux bash shell-script shell





      share







      New contributor




      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      user315606

      6




      6




      New contributor




      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user315606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          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
          );



          );






          user315606 is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475207%2fshell-script-to-assign-variable-to-an-environment-variable-specified-in-command%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user315606 is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          user315606 is a new contributor. Be nice, and check out our Code of Conduct.












          user315606 is a new contributor. Be nice, and check out our Code of Conduct.











          user315606 is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475207%2fshell-script-to-assign-variable-to-an-environment-variable-specified-in-command%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?