Get a boolean from test expression
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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");
bash shell-script test
add a comment |Â
up vote
1
down vote
favorite
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");
bash shell-script test
looks good to me, thx :)
â Alexander Mills
May 8 at 17:36
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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");
bash shell-script test
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");
bash shell-script test
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
add a comment |Â
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
add a comment |Â
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.
- First the variables
r2g_multi_temp
andr2g_keep_temp
will be replaced by their values. - Now the subshell will try to execute the value of the
r2g_multi_temp
. - 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 ther2g_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 itsstdout
will be appended on the possiblestdout
of the command executed from the value ofr2g_keep_temp
and everything will be stored inr2g_multi
.
- That command is executed succesfully and its
All in all, run away from this :D
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 return1
while-v foo
(note that there is no$
) will return0
. 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
add a comment |Â
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.
- First the variables
r2g_multi_temp
andr2g_keep_temp
will be replaced by their values. - Now the subshell will try to execute the value of the
r2g_multi_temp
. - 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 ther2g_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 itsstdout
will be appended on the possiblestdout
of the command executed from the value ofr2g_keep_temp
and everything will be stored inr2g_multi
.
- That command is executed succesfully and its
All in all, run away from this :D
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 return1
while-v foo
(note that there is no$
) will return0
. 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
add a comment |Â
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.
- First the variables
r2g_multi_temp
andr2g_keep_temp
will be replaced by their values. - Now the subshell will try to execute the value of the
r2g_multi_temp
. - 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 ther2g_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 itsstdout
will be appended on the possiblestdout
of the command executed from the value ofr2g_keep_temp
and everything will be stored inr2g_multi
.
- That command is executed succesfully and its
All in all, run away from this :D
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 return1
while-v foo
(note that there is no$
) will return0
. 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
add a comment |Â
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.
- First the variables
r2g_multi_temp
andr2g_keep_temp
will be replaced by their values. - Now the subshell will try to execute the value of the
r2g_multi_temp
. - 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 ther2g_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 itsstdout
will be appended on the possiblestdout
of the command executed from the value ofr2g_keep_temp
and everything will be stored inr2g_multi
.
- That command is executed succesfully and its
All in all, run away from this :D
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.
- First the variables
r2g_multi_temp
andr2g_keep_temp
will be replaced by their values. - Now the subshell will try to execute the value of the
r2g_multi_temp
. - 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 ther2g_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 itsstdout
will be appended on the possiblestdout
of the command executed from the value ofr2g_keep_temp
and everything will be stored inr2g_multi
.
- That command is executed succesfully and its
All in all, run away from this :D
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 return1
while-v foo
(note that there is no$
) will return0
. 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
add a comment |Â
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 return1
while-v foo
(note that there is no$
) will return0
. 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
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
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
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
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
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
looks good to me, thx :)
â Alexander Mills
May 8 at 17:36