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?
bash json
|
show 4 more comments
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?
bash json
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 be0
- true, or1
- 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
|
show 4 more comments
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?
bash json
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
bash json
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 be0
- true, or1
- 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
|
show 4 more comments
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 be0
- true, or1
- 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
|
show 4 more comments
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"
)'
add a comment |
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
add a comment |
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"
add a comment |
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"
)'
add a comment |
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"
)'
add a comment |
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"
)'
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"
)'
answered Nov 17 at 0:38
Dennis Williamson
1,88721119
1,88721119
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 16 at 21:01
answered Nov 16 at 20:02
PerlDuck
4,77611130
4,77611130
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 16 at 20:04
S_Flash
928117
928117
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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, or1
- 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