Capturing return code with test []
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
If I run this:
#!/usr/bin/env bash
simple_return_zero()
return 0;
simple_return_one()
return 1;
if [ simple_return_zero ]; then
echo "we have 0000";
fi
if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi
if [ simple_return_one ]; then
echo "we have 11111";
fi
if [ ! simple_return_one ]; then
echo "we have not 11111";
fi
I get:
we have 0000
we have 11111
I know the above is the wrong code to use, I think this is the right way to do it:
if simple_return_zero; then
echo "we have 0000";
fi
if ! simple_return_zero; then
echo "we have not 00000";
fi
if simple_return_one; then
echo "we have 11111";
fi
if ! simple_return_one; then
echo "we have not 11111";
fi
and now we get something more expected:
we have 0000
we have not 11111
My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???
bash shell-script test
add a comment |Â
up vote
1
down vote
favorite
If I run this:
#!/usr/bin/env bash
simple_return_zero()
return 0;
simple_return_one()
return 1;
if [ simple_return_zero ]; then
echo "we have 0000";
fi
if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi
if [ simple_return_one ]; then
echo "we have 11111";
fi
if [ ! simple_return_one ]; then
echo "we have not 11111";
fi
I get:
we have 0000
we have 11111
I know the above is the wrong code to use, I think this is the right way to do it:
if simple_return_zero; then
echo "we have 0000";
fi
if ! simple_return_zero; then
echo "we have not 00000";
fi
if simple_return_one; then
echo "we have 11111";
fi
if ! simple_return_one; then
echo "we have not 11111";
fi
and now we get something more expected:
we have 0000
we have not 11111
My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???
bash shell-script test
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If I run this:
#!/usr/bin/env bash
simple_return_zero()
return 0;
simple_return_one()
return 1;
if [ simple_return_zero ]; then
echo "we have 0000";
fi
if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi
if [ simple_return_one ]; then
echo "we have 11111";
fi
if [ ! simple_return_one ]; then
echo "we have not 11111";
fi
I get:
we have 0000
we have 11111
I know the above is the wrong code to use, I think this is the right way to do it:
if simple_return_zero; then
echo "we have 0000";
fi
if ! simple_return_zero; then
echo "we have not 00000";
fi
if simple_return_one; then
echo "we have 11111";
fi
if ! simple_return_one; then
echo "we have not 11111";
fi
and now we get something more expected:
we have 0000
we have not 11111
My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???
bash shell-script test
If I run this:
#!/usr/bin/env bash
simple_return_zero()
return 0;
simple_return_one()
return 1;
if [ simple_return_zero ]; then
echo "we have 0000";
fi
if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi
if [ simple_return_one ]; then
echo "we have 11111";
fi
if [ ! simple_return_one ]; then
echo "we have not 11111";
fi
I get:
we have 0000
we have 11111
I know the above is the wrong code to use, I think this is the right way to do it:
if simple_return_zero; then
echo "we have 0000";
fi
if ! simple_return_zero; then
echo "we have not 00000";
fi
if simple_return_one; then
echo "we have 11111";
fi
if ! simple_return_one; then
echo "we have not 11111";
fi
and now we get something more expected:
we have 0000
we have not 11111
My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???
bash shell-script test
asked Jun 14 at 0:41
Alexander Mills
1,873929
1,873929
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42
add a comment |Â
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Doesn't the test command check for exit codes / return codes???
Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test
.
if
on its own checks the return code of the command executed.
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
Fromhelp test
: "STRING True if string is not empty."
â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Doesn't the test command check for exit codes / return codes???
Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test
.
if
on its own checks the return code of the command executed.
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
Fromhelp test
: "STRING True if string is not empty."
â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
add a comment |Â
up vote
2
down vote
accepted
Doesn't the test command check for exit codes / return codes???
Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test
.
if
on its own checks the return code of the command executed.
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
Fromhelp test
: "STRING True if string is not empty."
â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Doesn't the test command check for exit codes / return codes???
Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test
.
if
on its own checks the return code of the command executed.
Doesn't the test command check for exit codes / return codes???
Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test
.
if
on its own checks the return code of the command executed.
answered Jun 14 at 0:46
Ignacio Vazquez-Abrams
31.9k66780
31.9k66780
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
Fromhelp test
: "STRING True if string is not empty."
â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
add a comment |Â
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
Fromhelp test
: "STRING True if string is not empty."
â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
â Alexander Mills
Jun 14 at 0:51
From
help test
: "STRING True if string is not empty."â Ignacio Vazquez-Abrams
Jun 14 at 0:51
From
help test
: "STRING True if string is not empty."â Ignacio Vazquez-Abrams
Jun 14 at 0:51
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
Ah yes that makes sense, fml
â Alexander Mills
Jun 14 at 0:52
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%2f449696%2fcapturing-return-code-with-test%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
also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
â Alexander Mills
Jun 14 at 0:42