Bash assign variable result of bool function, then check
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)
Here is a sample of what I am attempting
#!/bin/bash
_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi
_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi
_my_func
I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var
as nothing. Not a 0. Not null. Literally it is just blank.
bash shell-script
add a comment |Â
up vote
4
down vote
favorite
I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)
Here is a sample of what I am attempting
#!/bin/bash
_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi
_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi
_my_func
I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var
as nothing. Not a 0. Not null. Literally it is just blank.
bash shell-script
1
Note: In Bash if comparing strings, you must use double brackets like[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)
Here is a sample of what I am attempting
#!/bin/bash
_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi
_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi
_my_func
I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var
as nothing. Not a 0. Not null. Literally it is just blank.
bash shell-script
I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)
Here is a sample of what I am attempting
#!/bin/bash
_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi
_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi
_my_func
I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var
as nothing. Not a 0. Not null. Literally it is just blank.
bash shell-script
asked May 27 at 17:22
Byron Mansfield
628
628
1
Note: In Bash if comparing strings, you must use double brackets like[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02
add a comment |Â
1
Note: In Bash if comparing strings, you must use double brackets like[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02
1
1
Note: In Bash if comparing strings, you must use double brackets like
[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02
Note: In Bash if comparing strings, you must use double brackets like
[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
13
down vote
accepted
You confuse output with the exit code.
_my_func() {
if _has_string 'string'; then
You should also quote your variables; and _has_string
can be simplified:
_has_string()
[ "$1" = 'string' ]
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around theif _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
â Byron Mansfield
May 29 at 20:21
add a comment |Â
up vote
5
down vote
$(...)
returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $?
is set to the exit code.
So rather than
var=$(_has_string 'string')
...
consider
_has_string 'string'
var=$?
...
2
Or evenif _has_string 'string'; then ...
â roaima
May 27 at 20:42
add a comment |Â
up vote
0
down vote
As others already pointed out, what you need to merely use the function within the if
statement. Here's something you might not have known:
A function can be treated as a form of compound command. And according to bash manual [[
is one of the operators that can be used in construction of a compound command. Thus, your function _has_string()
can be implemented as so:
$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES
Of course, this is in no way portable, because of the [[
operator. But we can make use of ( )
or . Here's for example how this works in
/bin/dash
:
$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
You confuse output with the exit code.
_my_func() {
if _has_string 'string'; then
You should also quote your variables; and _has_string
can be simplified:
_has_string()
[ "$1" = 'string' ]
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around theif _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
â Byron Mansfield
May 29 at 20:21
add a comment |Â
up vote
13
down vote
accepted
You confuse output with the exit code.
_my_func() {
if _has_string 'string'; then
You should also quote your variables; and _has_string
can be simplified:
_has_string()
[ "$1" = 'string' ]
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around theif _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
â Byron Mansfield
May 29 at 20:21
add a comment |Â
up vote
13
down vote
accepted
up vote
13
down vote
accepted
You confuse output with the exit code.
_my_func() {
if _has_string 'string'; then
You should also quote your variables; and _has_string
can be simplified:
_has_string()
[ "$1" = 'string' ]
You confuse output with the exit code.
_my_func() {
if _has_string 'string'; then
You should also quote your variables; and _has_string
can be simplified:
_has_string()
[ "$1" = 'string' ]
edited May 29 at 11:15
Jeff Schaller
31k846105
31k846105
answered May 27 at 17:31
Hauke Laging
53.1k1282130
53.1k1282130
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around theif _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
â Byron Mansfield
May 29 at 20:21
add a comment |Â
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around theif _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
â Byron Mansfield
May 29 at 20:21
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the
if _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.â Byron Mansfield
May 29 at 20:21
You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the
if _has_string 'string'
conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.â Byron Mansfield
May 29 at 20:21
add a comment |Â
up vote
5
down vote
$(...)
returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $?
is set to the exit code.
So rather than
var=$(_has_string 'string')
...
consider
_has_string 'string'
var=$?
...
2
Or evenif _has_string 'string'; then ...
â roaima
May 27 at 20:42
add a comment |Â
up vote
5
down vote
$(...)
returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $?
is set to the exit code.
So rather than
var=$(_has_string 'string')
...
consider
_has_string 'string'
var=$?
...
2
Or evenif _has_string 'string'; then ...
â roaima
May 27 at 20:42
add a comment |Â
up vote
5
down vote
up vote
5
down vote
$(...)
returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $?
is set to the exit code.
So rather than
var=$(_has_string 'string')
...
consider
_has_string 'string'
var=$?
...
$(...)
returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $?
is set to the exit code.
So rather than
var=$(_has_string 'string')
...
consider
_has_string 'string'
var=$?
...
answered May 27 at 19:50
Silvio Mayolo
1513
1513
2
Or evenif _has_string 'string'; then ...
â roaima
May 27 at 20:42
add a comment |Â
2
Or evenif _has_string 'string'; then ...
â roaima
May 27 at 20:42
2
2
Or even
if _has_string 'string'; then ...
â roaima
May 27 at 20:42
Or even
if _has_string 'string'; then ...
â roaima
May 27 at 20:42
add a comment |Â
up vote
0
down vote
As others already pointed out, what you need to merely use the function within the if
statement. Here's something you might not have known:
A function can be treated as a form of compound command. And according to bash manual [[
is one of the operators that can be used in construction of a compound command. Thus, your function _has_string()
can be implemented as so:
$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES
Of course, this is in no way portable, because of the [[
operator. But we can make use of ( )
or . Here's for example how this works in
/bin/dash
:
$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
add a comment |Â
up vote
0
down vote
As others already pointed out, what you need to merely use the function within the if
statement. Here's something you might not have known:
A function can be treated as a form of compound command. And according to bash manual [[
is one of the operators that can be used in construction of a compound command. Thus, your function _has_string()
can be implemented as so:
$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES
Of course, this is in no way portable, because of the [[
operator. But we can make use of ( )
or . Here's for example how this works in
/bin/dash
:
$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As others already pointed out, what you need to merely use the function within the if
statement. Here's something you might not have known:
A function can be treated as a form of compound command. And according to bash manual [[
is one of the operators that can be used in construction of a compound command. Thus, your function _has_string()
can be implemented as so:
$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES
Of course, this is in no way portable, because of the [[
operator. But we can make use of ( )
or . Here's for example how this works in
/bin/dash
:
$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$
As others already pointed out, what you need to merely use the function within the if
statement. Here's something you might not have known:
A function can be treated as a form of compound command. And according to bash manual [[
is one of the operators that can be used in construction of a compound command. Thus, your function _has_string()
can be implemented as so:
$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES
Of course, this is in no way portable, because of the [[
operator. But we can make use of ( )
or . Here's for example how this works in
/bin/dash
:
$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$
answered May 28 at 5:47
Sergiy Kolodyazhnyy
7,56611545
7,56611545
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
add a comment |Â
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
1
1
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
â ilkkachu
May 29 at 11:28
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%2f446339%2fbash-assign-variable-result-of-bool-function-then-check%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
1
Note: In Bash if comparing strings, you must use double brackets like
[[ $1 == "string" ]];
â Vlastimil
May 28 at 7:02