How to set boolean value in curl command used bash script











up vote
1
down vote

favorite












I need set into variable true/false value and send him into json with curl command:



name=$1
sx=$2

`curl -d '{"name":"'"$name"'", "sex":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc`


Variable sx accepts the value male or female how to set the appropriate boolean value into sex variable?










share|improve this question




















  • 1




    Which one is the true sex? Male or female? Maybe there are other values?
    – PerlDuck
    Nov 16 at 19:50












  • @PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
    – Valentyn Hruzytskyi
    Nov 16 at 19:52








  • 4




    The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
    – Fabby
    Nov 16 at 20:48






  • 1




    Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
    – PerlDuck
    Nov 16 at 20:49






  • 1




    "sex":file-not-found
    – Dennis Williamson
    Nov 17 at 0:04















up vote
1
down vote

favorite












I need set into variable true/false value and send him into json with curl command:



name=$1
sx=$2

`curl -d '{"name":"'"$name"'", "sex":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc`


Variable sx accepts the value male or female how to set the appropriate boolean value into sex variable?










share|improve this question




















  • 1




    Which one is the true sex? Male or female? Maybe there are other values?
    – PerlDuck
    Nov 16 at 19:50












  • @PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
    – Valentyn Hruzytskyi
    Nov 16 at 19:52








  • 4




    The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
    – Fabby
    Nov 16 at 20:48






  • 1




    Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
    – PerlDuck
    Nov 16 at 20:49






  • 1




    "sex":file-not-found
    – Dennis Williamson
    Nov 17 at 0:04













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need set into variable true/false value and send him into json with curl command:



name=$1
sx=$2

`curl -d '{"name":"'"$name"'", "sex":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc`


Variable sx accepts the value male or female how to set the appropriate boolean value into sex variable?










share|improve this question















I need set into variable true/false value and send him into json with curl command:



name=$1
sx=$2

`curl -d '{"name":"'"$name"'", "sex":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc`


Variable sx accepts the value male or female how to set the appropriate boolean value into sex variable?







bash json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 8:59

























asked Nov 16 at 19:40









Valentyn Hruzytskyi

2009




2009








  • 1




    Which one is the true sex? Male or female? Maybe there are other values?
    – PerlDuck
    Nov 16 at 19:50












  • @PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
    – Valentyn Hruzytskyi
    Nov 16 at 19:52








  • 4




    The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
    – Fabby
    Nov 16 at 20:48






  • 1




    Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
    – PerlDuck
    Nov 16 at 20:49






  • 1




    "sex":file-not-found
    – Dennis Williamson
    Nov 17 at 0:04














  • 1




    Which one is the true sex? Male or female? Maybe there are other values?
    – PerlDuck
    Nov 16 at 19:50












  • @PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
    – Valentyn Hruzytskyi
    Nov 16 at 19:52








  • 4




    The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
    – Fabby
    Nov 16 at 20:48






  • 1




    Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
    – PerlDuck
    Nov 16 at 20:49






  • 1




    "sex":file-not-found
    – Dennis Williamson
    Nov 17 at 0:04








1




1




Which one is the true sex? Male or female? Maybe there are other values?
– PerlDuck
Nov 16 at 19:50






Which one is the true sex? Male or female? Maybe there are other values?
– PerlDuck
Nov 16 at 19:50














@PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
– Valentyn Hruzytskyi
Nov 16 at 19:52






@PerlDuck it is do not matter. It is can be 0 - true, or 1 - false
– Valentyn Hruzytskyi
Nov 16 at 19:52






4




4




The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
– Fabby
Nov 16 at 20:48




The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
– Fabby
Nov 16 at 20:48




1




1




Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
– PerlDuck
Nov 16 at 20:49




Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
– PerlDuck
Nov 16 at 20:49




1




1




"sex":file-not-found
– Dennis Williamson
Nov 17 at 0:04




"sex":file-not-found
– Dennis Williamson
Nov 17 at 0:04










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.



Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.



You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.



name=$1
sx=$2

declare -A sexes
sexes[male]=false
sexes[female]=true

json_string='{"name":"'
json_string+=$name
json_string+='", "sex":'
json_string+=${sexes[$sx]}
json_string+='}'

curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc


You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.



Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.



declare -A sexes='([female]="true" [male]="false")'

declare -A sexes='(
[female]="true"
[male]="false"
)'





share|improve this answer




























    up vote
    4
    down vote













    The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:



    if [ "$sx" = "male" ]; then
    sex="true"; # or whatever you consider male sex to be
    else
    sex="false"; # just the opposite, see above
    fi

    curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc





    share|improve this answer






























      up vote
      3
      down vote













      In your case you use json and can use text "true" and text "false".



      You need add to bash script logic:



      sex="false"
      if [ "$sx" = "male" ]; then
      sex="true"
      fi


      then run curl command with ... {"name":"'"$name"'", "sex":"$sex"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...



      Test script:



      #!/bin/bash

      sx="male"

      sex="false"
      if [ "$sx" = "male" ]; then
      sex="true"
      fi

      echo "$sex"





      share|improve this answer





















        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "89"
        };
        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: 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
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093550%2fhow-to-set-boolean-value-in-curl-command-used-bash-script%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.



        Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.



        You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.



        name=$1
        sx=$2

        declare -A sexes
        sexes[male]=false
        sexes[female]=true

        json_string='{"name":"'
        json_string+=$name
        json_string+='", "sex":'
        json_string+=${sexes[$sx]}
        json_string+='}'

        curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc


        You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.



        Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.



        declare -A sexes='([female]="true" [male]="false")'

        declare -A sexes='(
        [female]="true"
        [male]="false"
        )'





        share|improve this answer

























          up vote
          2
          down vote



          accepted










          Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.



          Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.



          You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.



          name=$1
          sx=$2

          declare -A sexes
          sexes[male]=false
          sexes[female]=true

          json_string='{"name":"'
          json_string+=$name
          json_string+='", "sex":'
          json_string+=${sexes[$sx]}
          json_string+='}'

          curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc


          You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.



          Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.



          declare -A sexes='([female]="true" [male]="false")'

          declare -A sexes='(
          [female]="true"
          [male]="false"
          )'





          share|improve this answer























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.



            Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.



            You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.



            name=$1
            sx=$2

            declare -A sexes
            sexes[male]=false
            sexes[female]=true

            json_string='{"name":"'
            json_string+=$name
            json_string+='", "sex":'
            json_string+=${sexes[$sx]}
            json_string+='}'

            curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc


            You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.



            Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.



            declare -A sexes='([female]="true" [male]="false")'

            declare -A sexes='(
            [female]="true"
            [male]="false"
            )'





            share|improve this answer












            Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.



            Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.



            You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.



            name=$1
            sx=$2

            declare -A sexes
            sexes[male]=false
            sexes[female]=true

            json_string='{"name":"'
            json_string+=$name
            json_string+='", "sex":'
            json_string+=${sexes[$sx]}
            json_string+='}'

            curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc


            You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.



            Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.



            declare -A sexes='([female]="true" [male]="false")'

            declare -A sexes='(
            [female]="true"
            [male]="false"
            )'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 at 0:38









            Dennis Williamson

            1,88721119




            1,88721119
























                up vote
                4
                down vote













                The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:



                if [ "$sx" = "male" ]; then
                sex="true"; # or whatever you consider male sex to be
                else
                sex="false"; # just the opposite, see above
                fi

                curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc





                share|improve this answer



























                  up vote
                  4
                  down vote













                  The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:



                  if [ "$sx" = "male" ]; then
                  sex="true"; # or whatever you consider male sex to be
                  else
                  sex="false"; # just the opposite, see above
                  fi

                  curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc





                  share|improve this answer

























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:



                    if [ "$sx" = "male" ]; then
                    sex="true"; # or whatever you consider male sex to be
                    else
                    sex="false"; # just the opposite, see above
                    fi

                    curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc





                    share|improve this answer














                    The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:



                    if [ "$sx" = "male" ]; then
                    sex="true"; # or whatever you consider male sex to be
                    else
                    sex="false"; # just the opposite, see above
                    fi

                    curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 16 at 21:01

























                    answered Nov 16 at 20:02









                    PerlDuck

                    4,83111130




                    4,83111130






















                        up vote
                        3
                        down vote













                        In your case you use json and can use text "true" and text "false".



                        You need add to bash script logic:



                        sex="false"
                        if [ "$sx" = "male" ]; then
                        sex="true"
                        fi


                        then run curl command with ... {"name":"'"$name"'", "sex":"$sex"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...



                        Test script:



                        #!/bin/bash

                        sx="male"

                        sex="false"
                        if [ "$sx" = "male" ]; then
                        sex="true"
                        fi

                        echo "$sex"





                        share|improve this answer

























                          up vote
                          3
                          down vote













                          In your case you use json and can use text "true" and text "false".



                          You need add to bash script logic:



                          sex="false"
                          if [ "$sx" = "male" ]; then
                          sex="true"
                          fi


                          then run curl command with ... {"name":"'"$name"'", "sex":"$sex"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...



                          Test script:



                          #!/bin/bash

                          sx="male"

                          sex="false"
                          if [ "$sx" = "male" ]; then
                          sex="true"
                          fi

                          echo "$sex"





                          share|improve this answer























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            In your case you use json and can use text "true" and text "false".



                            You need add to bash script logic:



                            sex="false"
                            if [ "$sx" = "male" ]; then
                            sex="true"
                            fi


                            then run curl command with ... {"name":"'"$name"'", "sex":"$sex"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...



                            Test script:



                            #!/bin/bash

                            sx="male"

                            sex="false"
                            if [ "$sx" = "male" ]; then
                            sex="true"
                            fi

                            echo "$sex"





                            share|improve this answer












                            In your case you use json and can use text "true" and text "false".



                            You need add to bash script logic:



                            sex="false"
                            if [ "$sx" = "male" ]; then
                            sex="true"
                            fi


                            then run curl command with ... {"name":"'"$name"'", "sex":"$sex"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...



                            Test script:



                            #!/bin/bash

                            sx="male"

                            sex="false"
                            if [ "$sx" = "male" ]; then
                            sex="true"
                            fi

                            echo "$sex"






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 at 20:04









                            S_Flash

                            1,007117




                            1,007117






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093550%2fhow-to-set-boolean-value-in-curl-command-used-bash-script%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

                                Quarter-circle Tiles

                                build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                                Mont Emei