How to automate and generalize the following command -when used into a bash script- for different parameters...












4















I want to use this command into a bash script where each time I will have a different array input containing the parameters?



knowing that I have an array (as input from the user) where each column contains "parameteri=valuei".



I want to get rid of the hardcoded aspect in introducing the name and the value of each parameter.



For instance, with this input:



"id=123,verbosity=high"


I will eventually get this final instruction:



curl -X POST JENKINS_URL/job/JOB_NAME/build 
--user USER:TOKEN
--data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'


What is a clean way to do so?










share|improve this question









New contributor




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

























    4















    I want to use this command into a bash script where each time I will have a different array input containing the parameters?



    knowing that I have an array (as input from the user) where each column contains "parameteri=valuei".



    I want to get rid of the hardcoded aspect in introducing the name and the value of each parameter.



    For instance, with this input:



    "id=123,verbosity=high"


    I will eventually get this final instruction:



    curl -X POST JENKINS_URL/job/JOB_NAME/build 
    --user USER:TOKEN
    --data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'


    What is a clean way to do so?










    share|improve this question









    New contributor




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























      4












      4








      4








      I want to use this command into a bash script where each time I will have a different array input containing the parameters?



      knowing that I have an array (as input from the user) where each column contains "parameteri=valuei".



      I want to get rid of the hardcoded aspect in introducing the name and the value of each parameter.



      For instance, with this input:



      "id=123,verbosity=high"


      I will eventually get this final instruction:



      curl -X POST JENKINS_URL/job/JOB_NAME/build 
      --user USER:TOKEN
      --data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'


      What is a clean way to do so?










      share|improve this question









      New contributor




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












      I want to use this command into a bash script where each time I will have a different array input containing the parameters?



      knowing that I have an array (as input from the user) where each column contains "parameteri=valuei".



      I want to get rid of the hardcoded aspect in introducing the name and the value of each parameter.



      For instance, with this input:



      "id=123,verbosity=high"


      I will eventually get this final instruction:



      curl -X POST JENKINS_URL/job/JOB_NAME/build 
      --user USER:TOKEN
      --data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'


      What is a clean way to do so?







      bash loops echo






      share|improve this question









      New contributor




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











      share|improve this question









      New contributor




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









      share|improve this question




      share|improve this question








      edited 2 days ago









      Bsquare

      3,24431033




      3,24431033






      New contributor




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









      asked 2 days ago









      Nizar LouhichiNizar Louhichi

      232




      232




      New contributor




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





      New contributor





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






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
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can make it the sexy way, building the jsonParameters from specified key=value parameters:



          #!/bin/bash

          jsonParameters=""
          while IFS=',' read -r -a parameterEntries; do
          for parameterEntry in "${parameterEntries[@]}"; do
          IFS='=' read -r key value <<< "$parameterEntry"
          [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters,"
          jsonParameters="$jsonParameters {"name":"$key", "value": "$value"}"
          done
          done <<< "$@"


          Explanations:




          • the first loop will create the array named parameterEntries, with all your specified parameters, each element will contain key=value

          • then, the second loop, which iterates on each element of this array, will extract key, and value of it

          • eventually, it is only syntax writting to get the JSON output you want

          • the [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters," is just here to add a separating coma, only if there is more than one element


          Then you simply have to use the $jsonParameters where you want:



          curl -X POST JENKINS_URL/job/JOB_NAME/build 
          --user USER:TOKEN
          --data-urlencode json="{"parameter": [$jsonParameters]}"





          share|improve this answer


























          • Thank you for your response @Bsquare, can you elaborate a quick explanation?

            – Nizar Louhichi
            2 days ago













          • I added explanations in my answer.

            – Bsquare
            2 days ago











          • Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

            – Nizar Louhichi
            2 days ago











          • Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

            – Bsquare
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          });


          }
          });






          Nizar Louhichi 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%2fstackoverflow.com%2fquestions%2f54252810%2fhow-to-automate-and-generalize-the-following-command-when-used-into-a-bash-scri%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









          0














          You can make it the sexy way, building the jsonParameters from specified key=value parameters:



          #!/bin/bash

          jsonParameters=""
          while IFS=',' read -r -a parameterEntries; do
          for parameterEntry in "${parameterEntries[@]}"; do
          IFS='=' read -r key value <<< "$parameterEntry"
          [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters,"
          jsonParameters="$jsonParameters {"name":"$key", "value": "$value"}"
          done
          done <<< "$@"


          Explanations:




          • the first loop will create the array named parameterEntries, with all your specified parameters, each element will contain key=value

          • then, the second loop, which iterates on each element of this array, will extract key, and value of it

          • eventually, it is only syntax writting to get the JSON output you want

          • the [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters," is just here to add a separating coma, only if there is more than one element


          Then you simply have to use the $jsonParameters where you want:



          curl -X POST JENKINS_URL/job/JOB_NAME/build 
          --user USER:TOKEN
          --data-urlencode json="{"parameter": [$jsonParameters]}"





          share|improve this answer


























          • Thank you for your response @Bsquare, can you elaborate a quick explanation?

            – Nizar Louhichi
            2 days ago













          • I added explanations in my answer.

            – Bsquare
            2 days ago











          • Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

            – Nizar Louhichi
            2 days ago











          • Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

            – Bsquare
            2 days ago
















          0














          You can make it the sexy way, building the jsonParameters from specified key=value parameters:



          #!/bin/bash

          jsonParameters=""
          while IFS=',' read -r -a parameterEntries; do
          for parameterEntry in "${parameterEntries[@]}"; do
          IFS='=' read -r key value <<< "$parameterEntry"
          [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters,"
          jsonParameters="$jsonParameters {"name":"$key", "value": "$value"}"
          done
          done <<< "$@"


          Explanations:




          • the first loop will create the array named parameterEntries, with all your specified parameters, each element will contain key=value

          • then, the second loop, which iterates on each element of this array, will extract key, and value of it

          • eventually, it is only syntax writting to get the JSON output you want

          • the [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters," is just here to add a separating coma, only if there is more than one element


          Then you simply have to use the $jsonParameters where you want:



          curl -X POST JENKINS_URL/job/JOB_NAME/build 
          --user USER:TOKEN
          --data-urlencode json="{"parameter": [$jsonParameters]}"





          share|improve this answer


























          • Thank you for your response @Bsquare, can you elaborate a quick explanation?

            – Nizar Louhichi
            2 days ago













          • I added explanations in my answer.

            – Bsquare
            2 days ago











          • Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

            – Nizar Louhichi
            2 days ago











          • Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

            – Bsquare
            2 days ago














          0












          0








          0







          You can make it the sexy way, building the jsonParameters from specified key=value parameters:



          #!/bin/bash

          jsonParameters=""
          while IFS=',' read -r -a parameterEntries; do
          for parameterEntry in "${parameterEntries[@]}"; do
          IFS='=' read -r key value <<< "$parameterEntry"
          [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters,"
          jsonParameters="$jsonParameters {"name":"$key", "value": "$value"}"
          done
          done <<< "$@"


          Explanations:




          • the first loop will create the array named parameterEntries, with all your specified parameters, each element will contain key=value

          • then, the second loop, which iterates on each element of this array, will extract key, and value of it

          • eventually, it is only syntax writting to get the JSON output you want

          • the [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters," is just here to add a separating coma, only if there is more than one element


          Then you simply have to use the $jsonParameters where you want:



          curl -X POST JENKINS_URL/job/JOB_NAME/build 
          --user USER:TOKEN
          --data-urlencode json="{"parameter": [$jsonParameters]}"





          share|improve this answer















          You can make it the sexy way, building the jsonParameters from specified key=value parameters:



          #!/bin/bash

          jsonParameters=""
          while IFS=',' read -r -a parameterEntries; do
          for parameterEntry in "${parameterEntries[@]}"; do
          IFS='=' read -r key value <<< "$parameterEntry"
          [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters,"
          jsonParameters="$jsonParameters {"name":"$key", "value": "$value"}"
          done
          done <<< "$@"


          Explanations:




          • the first loop will create the array named parameterEntries, with all your specified parameters, each element will contain key=value

          • then, the second loop, which iterates on each element of this array, will extract key, and value of it

          • eventually, it is only syntax writting to get the JSON output you want

          • the [ ! -z "$jsonParameters" ] && jsonParameters="$jsonParameters," is just here to add a separating coma, only if there is more than one element


          Then you simply have to use the $jsonParameters where you want:



          curl -X POST JENKINS_URL/job/JOB_NAME/build 
          --user USER:TOKEN
          --data-urlencode json="{"parameter": [$jsonParameters]}"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          BsquareBsquare

          3,24431033




          3,24431033













          • Thank you for your response @Bsquare, can you elaborate a quick explanation?

            – Nizar Louhichi
            2 days ago













          • I added explanations in my answer.

            – Bsquare
            2 days ago











          • Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

            – Nizar Louhichi
            2 days ago











          • Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

            – Bsquare
            2 days ago



















          • Thank you for your response @Bsquare, can you elaborate a quick explanation?

            – Nizar Louhichi
            2 days ago













          • I added explanations in my answer.

            – Bsquare
            2 days ago











          • Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

            – Nizar Louhichi
            2 days ago











          • Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

            – Bsquare
            2 days ago

















          Thank you for your response @Bsquare, can you elaborate a quick explanation?

          – Nizar Louhichi
          2 days ago







          Thank you for your response @Bsquare, can you elaborate a quick explanation?

          – Nizar Louhichi
          2 days ago















          I added explanations in my answer.

          – Bsquare
          2 days ago





          I added explanations in my answer.

          – Bsquare
          2 days ago













          Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

          – Nizar Louhichi
          2 days ago





          Good, so I was right when I got rid of the first loop because I already handled the array containing the parameters. Thanks man I appreciate it.

          – Nizar Louhichi
          2 days ago













          Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

          – Bsquare
          2 days ago





          Nice to read ;) May you consider giving me up-vote on my answer? (I'll then delete this then useless comment ;)).

          – Bsquare
          2 days ago










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










          draft saved

          draft discarded


















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













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












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
















          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f54252810%2fhow-to-automate-and-generalize-the-following-command-when-used-into-a-bash-scri%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

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre