Catching the last returned value in Unix
Clash Royale CLAN TAG#URR8PPP
please help me with this.
I am doing a grep test file.txt and I want to catch the whole value and print it.I am doing this,
grep test file.txt
if [ "$?" -eq 0]
then
echo success
else
echo fail
fi
In this code, whether the grep returns a value or not always the exit code is 0 as the command is successful.
I do not want to do the following as my actual code has repetitive greps, say 10 times and it is not working.
a=`grep test file.txt`
if [ "$a" -eq "" ]
then
echo fail
else
echo success
fi
I am looking for a simple solution like this,
echo $(anything) should catch the value.
shell-script
add a comment |
please help me with this.
I am doing a grep test file.txt and I want to catch the whole value and print it.I am doing this,
grep test file.txt
if [ "$?" -eq 0]
then
echo success
else
echo fail
fi
In this code, whether the grep returns a value or not always the exit code is 0 as the command is successful.
I do not want to do the following as my actual code has repetitive greps, say 10 times and it is not working.
a=`grep test file.txt`
if [ "$a" -eq "" ]
then
echo fail
else
echo success
fi
I am looking for a simple solution like this,
echo $(anything) should catch the value.
shell-script
Are you doing likefoo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the$?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?
– drewbenn
Feb 4 '16 at 18:26
add a comment |
please help me with this.
I am doing a grep test file.txt and I want to catch the whole value and print it.I am doing this,
grep test file.txt
if [ "$?" -eq 0]
then
echo success
else
echo fail
fi
In this code, whether the grep returns a value or not always the exit code is 0 as the command is successful.
I do not want to do the following as my actual code has repetitive greps, say 10 times and it is not working.
a=`grep test file.txt`
if [ "$a" -eq "" ]
then
echo fail
else
echo success
fi
I am looking for a simple solution like this,
echo $(anything) should catch the value.
shell-script
please help me with this.
I am doing a grep test file.txt and I want to catch the whole value and print it.I am doing this,
grep test file.txt
if [ "$?" -eq 0]
then
echo success
else
echo fail
fi
In this code, whether the grep returns a value or not always the exit code is 0 as the command is successful.
I do not want to do the following as my actual code has repetitive greps, say 10 times and it is not working.
a=`grep test file.txt`
if [ "$a" -eq "" ]
then
echo fail
else
echo success
fi
I am looking for a simple solution like this,
echo $(anything) should catch the value.
shell-script
shell-script
edited Mar 6 at 16:23
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked Feb 4 '16 at 17:28
rameshramesh
11
11
Are you doing likefoo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the$?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?
– drewbenn
Feb 4 '16 at 18:26
add a comment |
Are you doing likefoo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the$?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?
– drewbenn
Feb 4 '16 at 18:26
Are you doing like
foo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the $?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?– drewbenn
Feb 4 '16 at 18:26
Are you doing like
foo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the $?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?– drewbenn
Feb 4 '16 at 18:26
add a comment |
4 Answers
4
active
oldest
votes
Your results don't match mine
echo 'some test here' > file.txt
grep test file.txt
echo $? # returns 0
echo 'something else here' > file.txt
grep test file.txt
echo $? # returns 1
Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0
and ]
because otherwise you would be getting the error, -bash: [: missing `]'
and you didn't report that.)
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
However, as a style suggestion I would test the grep
command directly, like this
if grep test file.txt
then
echo success
else
echo fail
fi
It's just struck me that you might want to be using the return value from the grep
after your if
...fi
block. In that case just save the value and continue on:
grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
echo success
else
echo fail
fi
# Here $ss still contains the return value from grep
add a comment |
Your original code should work as expected if you add a space
between the 0
and the ]
.
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
add a comment |
grep test file.txt
if [ "$?" -eq 0]
then
echo success
exitcode=0
else
echo fail
exitcode=1
fi
exit $exitcode
I am not sure what you are looking for but if you want your script to exit with the same code as your grep
command did, this should work for you.
If you are looking for something else, you should try to explain yourself better.
add a comment |
Could pipe through awk instead of relying on the return code.
grep posix test_file.txt|awk 'BEGINmatches=0matches++ENDif(matches>0)print "success";elseprint "fail";'
Print only the first match:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=$0;ENDif(matches!="")print "success";print matches;elseprint "fail";'
And with the value catching feature where every grep match is shown:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=matches $0 "n";ENDif(matches!="")print "success";print matches;elseprint "fail";'
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2funix.stackexchange.com%2fquestions%2f259903%2fcatching-the-last-returned-value-in-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your results don't match mine
echo 'some test here' > file.txt
grep test file.txt
echo $? # returns 0
echo 'something else here' > file.txt
grep test file.txt
echo $? # returns 1
Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0
and ]
because otherwise you would be getting the error, -bash: [: missing `]'
and you didn't report that.)
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
However, as a style suggestion I would test the grep
command directly, like this
if grep test file.txt
then
echo success
else
echo fail
fi
It's just struck me that you might want to be using the return value from the grep
after your if
...fi
block. In that case just save the value and continue on:
grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
echo success
else
echo fail
fi
# Here $ss still contains the return value from grep
add a comment |
Your results don't match mine
echo 'some test here' > file.txt
grep test file.txt
echo $? # returns 0
echo 'something else here' > file.txt
grep test file.txt
echo $? # returns 1
Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0
and ]
because otherwise you would be getting the error, -bash: [: missing `]'
and you didn't report that.)
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
However, as a style suggestion I would test the grep
command directly, like this
if grep test file.txt
then
echo success
else
echo fail
fi
It's just struck me that you might want to be using the return value from the grep
after your if
...fi
block. In that case just save the value and continue on:
grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
echo success
else
echo fail
fi
# Here $ss still contains the return value from grep
add a comment |
Your results don't match mine
echo 'some test here' > file.txt
grep test file.txt
echo $? # returns 0
echo 'something else here' > file.txt
grep test file.txt
echo $? # returns 1
Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0
and ]
because otherwise you would be getting the error, -bash: [: missing `]'
and you didn't report that.)
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
However, as a style suggestion I would test the grep
command directly, like this
if grep test file.txt
then
echo success
else
echo fail
fi
It's just struck me that you might want to be using the return value from the grep
after your if
...fi
block. In that case just save the value and continue on:
grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
echo success
else
echo fail
fi
# Here $ss still contains the return value from grep
Your results don't match mine
echo 'some test here' > file.txt
grep test file.txt
echo $? # returns 0
echo 'something else here' > file.txt
grep test file.txt
echo $? # returns 1
Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0
and ]
because otherwise you would be getting the error, -bash: [: missing `]'
and you didn't report that.)
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
However, as a style suggestion I would test the grep
command directly, like this
if grep test file.txt
then
echo success
else
echo fail
fi
It's just struck me that you might want to be using the return value from the grep
after your if
...fi
block. In that case just save the value and continue on:
grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
echo success
else
echo fail
fi
# Here $ss still contains the return value from grep
edited Feb 4 '16 at 18:38
answered Feb 4 '16 at 18:20
roaimaroaima
46k758124
46k758124
add a comment |
add a comment |
Your original code should work as expected if you add a space
between the 0
and the ]
.
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
add a comment |
Your original code should work as expected if you add a space
between the 0
and the ]
.
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
add a comment |
Your original code should work as expected if you add a space
between the 0
and the ]
.
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
Your original code should work as expected if you add a space
between the 0
and the ]
.
grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
answered Feb 4 '16 at 18:17
Timothy MartinTimothy Martin
5,4142430
5,4142430
add a comment |
add a comment |
grep test file.txt
if [ "$?" -eq 0]
then
echo success
exitcode=0
else
echo fail
exitcode=1
fi
exit $exitcode
I am not sure what you are looking for but if you want your script to exit with the same code as your grep
command did, this should work for you.
If you are looking for something else, you should try to explain yourself better.
add a comment |
grep test file.txt
if [ "$?" -eq 0]
then
echo success
exitcode=0
else
echo fail
exitcode=1
fi
exit $exitcode
I am not sure what you are looking for but if you want your script to exit with the same code as your grep
command did, this should work for you.
If you are looking for something else, you should try to explain yourself better.
add a comment |
grep test file.txt
if [ "$?" -eq 0]
then
echo success
exitcode=0
else
echo fail
exitcode=1
fi
exit $exitcode
I am not sure what you are looking for but if you want your script to exit with the same code as your grep
command did, this should work for you.
If you are looking for something else, you should try to explain yourself better.
grep test file.txt
if [ "$?" -eq 0]
then
echo success
exitcode=0
else
echo fail
exitcode=1
fi
exit $exitcode
I am not sure what you are looking for but if you want your script to exit with the same code as your grep
command did, this should work for you.
If you are looking for something else, you should try to explain yourself better.
answered Feb 4 '16 at 17:41
MelBurslanMelBurslan
5,34611533
5,34611533
add a comment |
add a comment |
Could pipe through awk instead of relying on the return code.
grep posix test_file.txt|awk 'BEGINmatches=0matches++ENDif(matches>0)print "success";elseprint "fail";'
Print only the first match:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=$0;ENDif(matches!="")print "success";print matches;elseprint "fail";'
And with the value catching feature where every grep match is shown:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=matches $0 "n";ENDif(matches!="")print "success";print matches;elseprint "fail";'
add a comment |
Could pipe through awk instead of relying on the return code.
grep posix test_file.txt|awk 'BEGINmatches=0matches++ENDif(matches>0)print "success";elseprint "fail";'
Print only the first match:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=$0;ENDif(matches!="")print "success";print matches;elseprint "fail";'
And with the value catching feature where every grep match is shown:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=matches $0 "n";ENDif(matches!="")print "success";print matches;elseprint "fail";'
add a comment |
Could pipe through awk instead of relying on the return code.
grep posix test_file.txt|awk 'BEGINmatches=0matches++ENDif(matches>0)print "success";elseprint "fail";'
Print only the first match:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=$0;ENDif(matches!="")print "success";print matches;elseprint "fail";'
And with the value catching feature where every grep match is shown:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=matches $0 "n";ENDif(matches!="")print "success";print matches;elseprint "fail";'
Could pipe through awk instead of relying on the return code.
grep posix test_file.txt|awk 'BEGINmatches=0matches++ENDif(matches>0)print "success";elseprint "fail";'
Print only the first match:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=$0;ENDif(matches!="")print "success";print matches;elseprint "fail";'
And with the value catching feature where every grep match is shown:
grep testvalue test_file.txt|awk 'BEGINmatches="";matches=matches $0 "n";ENDif(matches!="")print "success";print matches;elseprint "fail";'
answered Feb 4 '16 at 17:49
kph0x1kph0x1
33026
33026
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2funix.stackexchange.com%2fquestions%2f259903%2fcatching-the-last-returned-value-in-unix%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
Are you doing like
foo | grep bar | grep baz
and you need to know if an arbitrary grep failed/succeeded (set -o pipefail
), or you want to save the$?
return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...
)?– drewbenn
Feb 4 '16 at 18:26