Get a boolean from test expression

Multi tool use
Multi tool use

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











up vote
1
down vote

favorite
1












I have this bash code:



 local r2g_keep_temp=$(r2g_match_arg "--keep" "$my_args[@]");
local r2g_multi_temp=$(r2g_match_arg "--multi" "$my_args[@]");

local r2g_multi=[ "$r2g_multi_temp" || "$r2g_keep_temp" ];


I just want r2g_multi to represent a boolean, if either $r2g_multi_temp or $r2g_keep_temp is defined. How can I do that? The above is syntactically invalid.



On the other hand, this is syntactically valid, but not sure if it's correct:



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");






share|improve this question





















  • looks good to me, thx :)
    – Alexander Mills
    May 8 at 17:36














up vote
1
down vote

favorite
1












I have this bash code:



 local r2g_keep_temp=$(r2g_match_arg "--keep" "$my_args[@]");
local r2g_multi_temp=$(r2g_match_arg "--multi" "$my_args[@]");

local r2g_multi=[ "$r2g_multi_temp" || "$r2g_keep_temp" ];


I just want r2g_multi to represent a boolean, if either $r2g_multi_temp or $r2g_keep_temp is defined. How can I do that? The above is syntactically invalid.



On the other hand, this is syntactically valid, but not sure if it's correct:



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");






share|improve this question





















  • looks good to me, thx :)
    – Alexander Mills
    May 8 at 17:36












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have this bash code:



 local r2g_keep_temp=$(r2g_match_arg "--keep" "$my_args[@]");
local r2g_multi_temp=$(r2g_match_arg "--multi" "$my_args[@]");

local r2g_multi=[ "$r2g_multi_temp" || "$r2g_keep_temp" ];


I just want r2g_multi to represent a boolean, if either $r2g_multi_temp or $r2g_keep_temp is defined. How can I do that? The above is syntactically invalid.



On the other hand, this is syntactically valid, but not sure if it's correct:



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");






share|improve this question













I have this bash code:



 local r2g_keep_temp=$(r2g_match_arg "--keep" "$my_args[@]");
local r2g_multi_temp=$(r2g_match_arg "--multi" "$my_args[@]");

local r2g_multi=[ "$r2g_multi_temp" || "$r2g_keep_temp" ];


I just want r2g_multi to represent a boolean, if either $r2g_multi_temp or $r2g_keep_temp is defined. How can I do that? The above is syntactically invalid.



On the other hand, this is syntactically valid, but not sure if it's correct:



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");








share|improve this question












share|improve this question




share|improve this question








edited May 7 at 23:06
























asked May 7 at 22:59









Alexander Mills

1,885929




1,885929











  • looks good to me, thx :)
    – Alexander Mills
    May 8 at 17:36
















  • looks good to me, thx :)
    – Alexander Mills
    May 8 at 17:36















looks good to me, thx :)
– Alexander Mills
May 8 at 17:36




looks good to me, thx :)
– Alexander Mills
May 8 at 17:36










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There are no booleans in the shell scripts.



Best simulation for them are integer values.

For example, 0 for true and 1 for false.



[[ -v variable_name ]] will return 1 if the variable_name was not defined or 0 if it was.



Therefore, you can get your desired behavior with this:



// If one of the variables is defined, return value of this command is 0.
[[ -v r2g_keep_temp ]] || [[ -v r2g_multi_temp ]]

// Save the return value of the last command (0 or 1).
local r2g_multi="$?"


Of courese, you can interpret the numbers however you like, I just wanted to demonstrate one example.




By the way,



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");


is not the thing that you wish.



  1. First the variables r2g_multi_temp and r2g_keep_temp will be replaced by their values.

  2. Now the subshell will try to execute the value of the r2g_multi_temp.

  3. If by some miracle that value is a valid bash command there are 2 cases:

    • That command is executed succesfully and its stdout is saved in the r2g_multi.

    • That command failed and subshell invoked the value of the r2g_multi_temp.

      Similar story again, if it is a valid command it will be executed and its stdout will be appended on the possible stdout of the command executed from the value of r2g_keep_temp and everything will be stored in r2g_multi.


All in all, run away from this :D






share|improve this answer























  • nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
    – Alexander Mills
    May 7 at 23:27










  • "0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
    – Alexander Mills
    May 7 at 23:28






  • 1




    foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
    – Iskustvo
    May 7 at 23:57











  • nevermind, I was just confused between -v -z -n
    – Alexander Mills
    May 8 at 0:03










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



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442421%2fget-a-boolean-from-test-expression%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










There are no booleans in the shell scripts.



Best simulation for them are integer values.

For example, 0 for true and 1 for false.



[[ -v variable_name ]] will return 1 if the variable_name was not defined or 0 if it was.



Therefore, you can get your desired behavior with this:



// If one of the variables is defined, return value of this command is 0.
[[ -v r2g_keep_temp ]] || [[ -v r2g_multi_temp ]]

// Save the return value of the last command (0 or 1).
local r2g_multi="$?"


Of courese, you can interpret the numbers however you like, I just wanted to demonstrate one example.




By the way,



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");


is not the thing that you wish.



  1. First the variables r2g_multi_temp and r2g_keep_temp will be replaced by their values.

  2. Now the subshell will try to execute the value of the r2g_multi_temp.

  3. If by some miracle that value is a valid bash command there are 2 cases:

    • That command is executed succesfully and its stdout is saved in the r2g_multi.

    • That command failed and subshell invoked the value of the r2g_multi_temp.

      Similar story again, if it is a valid command it will be executed and its stdout will be appended on the possible stdout of the command executed from the value of r2g_keep_temp and everything will be stored in r2g_multi.


All in all, run away from this :D






share|improve this answer























  • nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
    – Alexander Mills
    May 7 at 23:27










  • "0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
    – Alexander Mills
    May 7 at 23:28






  • 1




    foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
    – Iskustvo
    May 7 at 23:57











  • nevermind, I was just confused between -v -z -n
    – Alexander Mills
    May 8 at 0:03














up vote
1
down vote



accepted










There are no booleans in the shell scripts.



Best simulation for them are integer values.

For example, 0 for true and 1 for false.



[[ -v variable_name ]] will return 1 if the variable_name was not defined or 0 if it was.



Therefore, you can get your desired behavior with this:



// If one of the variables is defined, return value of this command is 0.
[[ -v r2g_keep_temp ]] || [[ -v r2g_multi_temp ]]

// Save the return value of the last command (0 or 1).
local r2g_multi="$?"


Of courese, you can interpret the numbers however you like, I just wanted to demonstrate one example.




By the way,



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");


is not the thing that you wish.



  1. First the variables r2g_multi_temp and r2g_keep_temp will be replaced by their values.

  2. Now the subshell will try to execute the value of the r2g_multi_temp.

  3. If by some miracle that value is a valid bash command there are 2 cases:

    • That command is executed succesfully and its stdout is saved in the r2g_multi.

    • That command failed and subshell invoked the value of the r2g_multi_temp.

      Similar story again, if it is a valid command it will be executed and its stdout will be appended on the possible stdout of the command executed from the value of r2g_keep_temp and everything will be stored in r2g_multi.


All in all, run away from this :D






share|improve this answer























  • nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
    – Alexander Mills
    May 7 at 23:27










  • "0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
    – Alexander Mills
    May 7 at 23:28






  • 1




    foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
    – Iskustvo
    May 7 at 23:57











  • nevermind, I was just confused between -v -z -n
    – Alexander Mills
    May 8 at 0:03












up vote
1
down vote



accepted







up vote
1
down vote



accepted






There are no booleans in the shell scripts.



Best simulation for them are integer values.

For example, 0 for true and 1 for false.



[[ -v variable_name ]] will return 1 if the variable_name was not defined or 0 if it was.



Therefore, you can get your desired behavior with this:



// If one of the variables is defined, return value of this command is 0.
[[ -v r2g_keep_temp ]] || [[ -v r2g_multi_temp ]]

// Save the return value of the last command (0 or 1).
local r2g_multi="$?"


Of courese, you can interpret the numbers however you like, I just wanted to demonstrate one example.




By the way,



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");


is not the thing that you wish.



  1. First the variables r2g_multi_temp and r2g_keep_temp will be replaced by their values.

  2. Now the subshell will try to execute the value of the r2g_multi_temp.

  3. If by some miracle that value is a valid bash command there are 2 cases:

    • That command is executed succesfully and its stdout is saved in the r2g_multi.

    • That command failed and subshell invoked the value of the r2g_multi_temp.

      Similar story again, if it is a valid command it will be executed and its stdout will be appended on the possible stdout of the command executed from the value of r2g_keep_temp and everything will be stored in r2g_multi.


All in all, run away from this :D






share|improve this answer















There are no booleans in the shell scripts.



Best simulation for them are integer values.

For example, 0 for true and 1 for false.



[[ -v variable_name ]] will return 1 if the variable_name was not defined or 0 if it was.



Therefore, you can get your desired behavior with this:



// If one of the variables is defined, return value of this command is 0.
[[ -v r2g_keep_temp ]] || [[ -v r2g_multi_temp ]]

// Save the return value of the last command (0 or 1).
local r2g_multi="$?"


Of courese, you can interpret the numbers however you like, I just wanted to demonstrate one example.




By the way,



local r2g_multi=$("$r2g_multi_temp" || "$r2g_keep_temp");


is not the thing that you wish.



  1. First the variables r2g_multi_temp and r2g_keep_temp will be replaced by their values.

  2. Now the subshell will try to execute the value of the r2g_multi_temp.

  3. If by some miracle that value is a valid bash command there are 2 cases:

    • That command is executed succesfully and its stdout is saved in the r2g_multi.

    • That command failed and subshell invoked the value of the r2g_multi_temp.

      Similar story again, if it is a valid command it will be executed and its stdout will be appended on the possible stdout of the command executed from the value of r2g_keep_temp and everything will be stored in r2g_multi.


All in all, run away from this :D







share|improve this answer















share|improve this answer



share|improve this answer








edited May 7 at 23:51


























answered May 7 at 23:22









Iskustvo

667118




667118











  • nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
    – Alexander Mills
    May 7 at 23:27










  • "0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
    – Alexander Mills
    May 7 at 23:28






  • 1




    foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
    – Iskustvo
    May 7 at 23:57











  • nevermind, I was just confused between -v -z -n
    – Alexander Mills
    May 8 at 0:03
















  • nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
    – Alexander Mills
    May 7 at 23:27










  • "0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
    – Alexander Mills
    May 7 at 23:28






  • 1




    foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
    – Iskustvo
    May 7 at 23:57











  • nevermind, I was just confused between -v -z -n
    – Alexander Mills
    May 8 at 0:03















nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
– Alexander Mills
May 7 at 23:27




nice, what's the difference between -n $foo and -v $foo? I just heard of using -n to check if a variable is non empty
– Alexander Mills
May 7 at 23:27












"0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
– Alexander Mills
May 7 at 23:28




"0" and "1" are both defined though, so I need to return either "" or "1" not "0" and "1"
– Alexander Mills
May 7 at 23:28




1




1




foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
– Iskustvo
May 7 at 23:57





foo="", for this variable -n $foo will return 1 while -v foo (note that there is no $) will return 0. I don't understand the second quetion, can you explain it a bit more?
– Iskustvo
May 7 at 23:57













nevermind, I was just confused between -v -z -n
– Alexander Mills
May 8 at 0:03




nevermind, I was just confused between -v -z -n
– Alexander Mills
May 8 at 0:03












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442421%2fget-a-boolean-from-test-expression%23new-answer', 'question_page');

);

Post as a guest













































































hnQpq,rXLTboi7S7bYJ2uRC0oK N,XB6 G8tmhDog a8z94O677ncRR1vkgoI1t52DkCFD
Pn,PrqMooGxeVxp,Ea Cz YxuxiQG6gAp,acEEQjzc6Gwk QbBym,QgEl1DnikQob9DozBycam MHdG aQHkfRPvaQ Xxy

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS