Java-like properties file to environment variables

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












0















I need to convert each property of a properties file to a environment variable:



I mean, guess this property file:



mongo.port=27017
mongo.host=127.0.0.1
mongo.databaseName = test


bo.url-jwt=https://localhost:8089/token



I'd like to create them as environment variables as



export MONGO_PORT=27017
export MONGO_HOST=127.0.0.1
export MONGO_DATABASENAME=test
export BO_URL_JWT=https://localhost:8089/token


Any tool or script in order to get this?



I'm using ubuntu 18x + zsh










share|improve this question


























    0















    I need to convert each property of a properties file to a environment variable:



    I mean, guess this property file:



    mongo.port=27017
    mongo.host=127.0.0.1
    mongo.databaseName = test


    bo.url-jwt=https://localhost:8089/token



    I'd like to create them as environment variables as



    export MONGO_PORT=27017
    export MONGO_HOST=127.0.0.1
    export MONGO_DATABASENAME=test
    export BO_URL_JWT=https://localhost:8089/token


    Any tool or script in order to get this?



    I'm using ubuntu 18x + zsh










    share|improve this question
























      0












      0








      0








      I need to convert each property of a properties file to a environment variable:



      I mean, guess this property file:



      mongo.port=27017
      mongo.host=127.0.0.1
      mongo.databaseName = test


      bo.url-jwt=https://localhost:8089/token



      I'd like to create them as environment variables as



      export MONGO_PORT=27017
      export MONGO_HOST=127.0.0.1
      export MONGO_DATABASENAME=test
      export BO_URL_JWT=https://localhost:8089/token


      Any tool or script in order to get this?



      I'm using ubuntu 18x + zsh










      share|improve this question














      I need to convert each property of a properties file to a environment variable:



      I mean, guess this property file:



      mongo.port=27017
      mongo.host=127.0.0.1
      mongo.databaseName = test


      bo.url-jwt=https://localhost:8089/token



      I'd like to create them as environment variables as



      export MONGO_PORT=27017
      export MONGO_HOST=127.0.0.1
      export MONGO_DATABASENAME=test
      export BO_URL_JWT=https://localhost:8089/token


      Any tool or script in order to get this?



      I'm using ubuntu 18x + zsh







      shell-script environment-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 29 at 9:35









      JordiJordi

      1486




      1486




















          1 Answer
          1






          active

          oldest

          votes


















          1














          Here is a short script that can do this:



          #!/bin/bash
          tmp1=$(mktemp)
          tmp2=$(mktemp)

          cut -d= -f 1 props.txt | tr '[:lower:]' '[:upper:]' | tr '.-' '_' > $tmp1
          cut -d= -f 2 props.txt > $tmp2

          paste -d= $tmp1 $tmp2 > output.props

          sed -i 's/ *= */=/' output.props
          sed -i 's/^/export /' output.props

          rm $tmp1 $tmp2


          This script works in the following manner. Two temporary files are generated using mktemp. The input properties file is split on the '=' character and the two respective fields are sent to these two files.



          On the first field (the property name), the tr command is applied twice. Once for lowercase-to-uppercase conversion, once for changing the special characters to an underscore. The field values are only written to the temporary file after these changes.



          Once the processing is done, the paste command is used to the put the fields back together into a single file, output.props. Then, two sed commands are used. The first one removes any space characters around the '=' character, which would otherwise be a syntax error for the export command. The second sed command is used to prefix the string 'export ' to each line. Finally, the two temporary files are cleaned up.



          Sample output:



          $ cat output.props
          export MONGO_PORT=27017
          export MONGO_HOST=127.0.0.1
          export MONGO_DATABASENAME=test
          export BO_URL_JWT=https://localhost:8089/token


          Once the output file is generated, source it to actually set the variables in the environment. One of the two following syntaxes may be used for this:



          . output.props
          source output.props





          share|improve this answer

























          • Could you show on post, how to populate generated output.props to env variables then?

            – Jordi
            Jan 29 at 13:29











          • @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

            – Haxiel
            Jan 29 at 14:20










          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f497390%2fjava-like-properties-file-to-environment-variables%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Here is a short script that can do this:



          #!/bin/bash
          tmp1=$(mktemp)
          tmp2=$(mktemp)

          cut -d= -f 1 props.txt | tr '[:lower:]' '[:upper:]' | tr '.-' '_' > $tmp1
          cut -d= -f 2 props.txt > $tmp2

          paste -d= $tmp1 $tmp2 > output.props

          sed -i 's/ *= */=/' output.props
          sed -i 's/^/export /' output.props

          rm $tmp1 $tmp2


          This script works in the following manner. Two temporary files are generated using mktemp. The input properties file is split on the '=' character and the two respective fields are sent to these two files.



          On the first field (the property name), the tr command is applied twice. Once for lowercase-to-uppercase conversion, once for changing the special characters to an underscore. The field values are only written to the temporary file after these changes.



          Once the processing is done, the paste command is used to the put the fields back together into a single file, output.props. Then, two sed commands are used. The first one removes any space characters around the '=' character, which would otherwise be a syntax error for the export command. The second sed command is used to prefix the string 'export ' to each line. Finally, the two temporary files are cleaned up.



          Sample output:



          $ cat output.props
          export MONGO_PORT=27017
          export MONGO_HOST=127.0.0.1
          export MONGO_DATABASENAME=test
          export BO_URL_JWT=https://localhost:8089/token


          Once the output file is generated, source it to actually set the variables in the environment. One of the two following syntaxes may be used for this:



          . output.props
          source output.props





          share|improve this answer

























          • Could you show on post, how to populate generated output.props to env variables then?

            – Jordi
            Jan 29 at 13:29











          • @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

            – Haxiel
            Jan 29 at 14:20















          1














          Here is a short script that can do this:



          #!/bin/bash
          tmp1=$(mktemp)
          tmp2=$(mktemp)

          cut -d= -f 1 props.txt | tr '[:lower:]' '[:upper:]' | tr '.-' '_' > $tmp1
          cut -d= -f 2 props.txt > $tmp2

          paste -d= $tmp1 $tmp2 > output.props

          sed -i 's/ *= */=/' output.props
          sed -i 's/^/export /' output.props

          rm $tmp1 $tmp2


          This script works in the following manner. Two temporary files are generated using mktemp. The input properties file is split on the '=' character and the two respective fields are sent to these two files.



          On the first field (the property name), the tr command is applied twice. Once for lowercase-to-uppercase conversion, once for changing the special characters to an underscore. The field values are only written to the temporary file after these changes.



          Once the processing is done, the paste command is used to the put the fields back together into a single file, output.props. Then, two sed commands are used. The first one removes any space characters around the '=' character, which would otherwise be a syntax error for the export command. The second sed command is used to prefix the string 'export ' to each line. Finally, the two temporary files are cleaned up.



          Sample output:



          $ cat output.props
          export MONGO_PORT=27017
          export MONGO_HOST=127.0.0.1
          export MONGO_DATABASENAME=test
          export BO_URL_JWT=https://localhost:8089/token


          Once the output file is generated, source it to actually set the variables in the environment. One of the two following syntaxes may be used for this:



          . output.props
          source output.props





          share|improve this answer

























          • Could you show on post, how to populate generated output.props to env variables then?

            – Jordi
            Jan 29 at 13:29











          • @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

            – Haxiel
            Jan 29 at 14:20













          1












          1








          1







          Here is a short script that can do this:



          #!/bin/bash
          tmp1=$(mktemp)
          tmp2=$(mktemp)

          cut -d= -f 1 props.txt | tr '[:lower:]' '[:upper:]' | tr '.-' '_' > $tmp1
          cut -d= -f 2 props.txt > $tmp2

          paste -d= $tmp1 $tmp2 > output.props

          sed -i 's/ *= */=/' output.props
          sed -i 's/^/export /' output.props

          rm $tmp1 $tmp2


          This script works in the following manner. Two temporary files are generated using mktemp. The input properties file is split on the '=' character and the two respective fields are sent to these two files.



          On the first field (the property name), the tr command is applied twice. Once for lowercase-to-uppercase conversion, once for changing the special characters to an underscore. The field values are only written to the temporary file after these changes.



          Once the processing is done, the paste command is used to the put the fields back together into a single file, output.props. Then, two sed commands are used. The first one removes any space characters around the '=' character, which would otherwise be a syntax error for the export command. The second sed command is used to prefix the string 'export ' to each line. Finally, the two temporary files are cleaned up.



          Sample output:



          $ cat output.props
          export MONGO_PORT=27017
          export MONGO_HOST=127.0.0.1
          export MONGO_DATABASENAME=test
          export BO_URL_JWT=https://localhost:8089/token


          Once the output file is generated, source it to actually set the variables in the environment. One of the two following syntaxes may be used for this:



          . output.props
          source output.props





          share|improve this answer















          Here is a short script that can do this:



          #!/bin/bash
          tmp1=$(mktemp)
          tmp2=$(mktemp)

          cut -d= -f 1 props.txt | tr '[:lower:]' '[:upper:]' | tr '.-' '_' > $tmp1
          cut -d= -f 2 props.txt > $tmp2

          paste -d= $tmp1 $tmp2 > output.props

          sed -i 's/ *= */=/' output.props
          sed -i 's/^/export /' output.props

          rm $tmp1 $tmp2


          This script works in the following manner. Two temporary files are generated using mktemp. The input properties file is split on the '=' character and the two respective fields are sent to these two files.



          On the first field (the property name), the tr command is applied twice. Once for lowercase-to-uppercase conversion, once for changing the special characters to an underscore. The field values are only written to the temporary file after these changes.



          Once the processing is done, the paste command is used to the put the fields back together into a single file, output.props. Then, two sed commands are used. The first one removes any space characters around the '=' character, which would otherwise be a syntax error for the export command. The second sed command is used to prefix the string 'export ' to each line. Finally, the two temporary files are cleaned up.



          Sample output:



          $ cat output.props
          export MONGO_PORT=27017
          export MONGO_HOST=127.0.0.1
          export MONGO_DATABASENAME=test
          export BO_URL_JWT=https://localhost:8089/token


          Once the output file is generated, source it to actually set the variables in the environment. One of the two following syntaxes may be used for this:



          . output.props
          source output.props






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 29 at 14:19

























          answered Jan 29 at 11:54









          HaxielHaxiel

          2,7401915




          2,7401915












          • Could you show on post, how to populate generated output.props to env variables then?

            – Jordi
            Jan 29 at 13:29











          • @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

            – Haxiel
            Jan 29 at 14:20

















          • Could you show on post, how to populate generated output.props to env variables then?

            – Jordi
            Jan 29 at 13:29











          • @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

            – Haxiel
            Jan 29 at 14:20
















          Could you show on post, how to populate generated output.props to env variables then?

          – Jordi
          Jan 29 at 13:29





          Could you show on post, how to populate generated output.props to env variables then?

          – Jordi
          Jan 29 at 13:29













          @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

          – Haxiel
          Jan 29 at 14:20





          @Jordi I've edited my answer with this information. Do note that I have added one more sed command to clean up the spaces from the input as well.

          – Haxiel
          Jan 29 at 14:20

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497390%2fjava-like-properties-file-to-environment-variables%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          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?