Catching the last returned value in Unix

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












0















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.










share|improve this question
























  • 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















0















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.










share|improve this question
























  • 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













0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















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










4 Answers
4






active

oldest

votes


















4














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





share|improve this answer
































    2














    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





    share|improve this answer






























      0














      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.






      share|improve this answer






























        0














        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";'





        share|improve this answer























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



          );













          draft saved

          draft discarded


















          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









          4














          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





          share|improve this answer





























            4














            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





            share|improve this answer



























              4












              4








              4







              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





              share|improve this answer















              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 4 '16 at 18:38

























              answered Feb 4 '16 at 18:20









              roaimaroaima

              46k758124




              46k758124























                  2














                  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





                  share|improve this answer



























                    2














                    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





                    share|improve this answer

























                      2












                      2








                      2







                      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





                      share|improve this answer













                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 4 '16 at 18:17









                      Timothy MartinTimothy Martin

                      5,4142430




                      5,4142430





















                          0














                          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.






                          share|improve this answer



























                            0














                            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.






                            share|improve this answer

























                              0












                              0








                              0







                              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.






                              share|improve this answer













                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 4 '16 at 17:41









                              MelBurslanMelBurslan

                              5,34611533




                              5,34611533





















                                  0














                                  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";'





                                  share|improve this answer



























                                    0














                                    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";'





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      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";'





                                      share|improve this answer













                                      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";'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 4 '16 at 17:49









                                      kph0x1kph0x1

                                      33026




                                      33026



























                                          draft saved

                                          draft discarded
















































                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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






                                          Popular posts from this blog

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

                                          Bahrain

                                          Postfix configuration issue with fips on centos 7; mailgun relay