Writing a script to check if the output of a command is “3”

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











up vote
2
down vote

favorite












I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.



#!/bin/bash

Server_Count=""
nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
if [ $? == 3 ]; then
Server_Count="$?"
else
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com

fi









share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
    I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.



    #!/bin/bash

    Server_Count=""
    nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
    if [ $? == 3 ]; then
    Server_Count="$?"
    else
    echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com

    fi









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
      I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.



      #!/bin/bash

      Server_Count=""
      nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
      if [ $? == 3 ]; then
      Server_Count="$?"
      else
      echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com

      fi









      share|improve this question















      I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
      I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.



      #!/bin/bash

      Server_Count=""
      nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
      if [ $? == 3 ]; then
      Server_Count="$?"
      else
      echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com

      fi






      shell-script scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 13 at 13:56









      Jeff Schaller

      32.4k849110




      32.4k849110










      asked Aug 10 at 21:16









      Katkota

      384




      384




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          $? will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $? will be 0.



          You want command substitution:



          #!/bin/bash

          server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
          if [[ "$server_count" -ne 3 ]]; then
          echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
          fi


          This will set server_count to the results of wc -l so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).



          Also the -ne check is being used here which is the correct check for integer comparison.



          On a side note you were using the POSIX shell test [ ... ] with a bash comparison operator ==. This will still work on many systems but beware when using [ ... ] you should use = and when using [[ ... ]] you can use either = or ==.






          share|improve this answer






















          • grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
            – glenn jackman
            Aug 13 at 13:49










          • And another grep after that: | grep -qxF 3 ... and you don't need the variable.
            – Kusalananda
            Aug 13 at 14:03

















          up vote
          0
          down vote













          As Jesse_b explained, your code does not work since you are looking at the return status of wc -l rather than the data returned from it.



          if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
          echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
          fi


          This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup, and if that count is not exactly three, it will send an email.



          With -c, grep will output the number of lines that matches the given pattern. The -q option to grep stops grep from generating output. Here, we're only interested in whether grep matches the 3 or not, and we check this by its return status. With -x we force a match across a whole line (i.e. we will match only 3 and not 30). The -F option makes grep do a string match instead of a regular expression match.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f461905%2fwriting-a-script-to-check-if-the-output-of-a-command-is-3%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            $? will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $? will be 0.



            You want command substitution:



            #!/bin/bash

            server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
            if [[ "$server_count" -ne 3 ]]; then
            echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
            fi


            This will set server_count to the results of wc -l so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).



            Also the -ne check is being used here which is the correct check for integer comparison.



            On a side note you were using the POSIX shell test [ ... ] with a bash comparison operator ==. This will still work on many systems but beware when using [ ... ] you should use = and when using [[ ... ]] you can use either = or ==.






            share|improve this answer






















            • grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
              – glenn jackman
              Aug 13 at 13:49










            • And another grep after that: | grep -qxF 3 ... and you don't need the variable.
              – Kusalananda
              Aug 13 at 14:03














            up vote
            4
            down vote



            accepted










            $? will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $? will be 0.



            You want command substitution:



            #!/bin/bash

            server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
            if [[ "$server_count" -ne 3 ]]; then
            echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
            fi


            This will set server_count to the results of wc -l so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).



            Also the -ne check is being used here which is the correct check for integer comparison.



            On a side note you were using the POSIX shell test [ ... ] with a bash comparison operator ==. This will still work on many systems but beware when using [ ... ] you should use = and when using [[ ... ]] you can use either = or ==.






            share|improve this answer






















            • grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
              – glenn jackman
              Aug 13 at 13:49










            • And another grep after that: | grep -qxF 3 ... and you don't need the variable.
              – Kusalananda
              Aug 13 at 14:03












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            $? will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $? will be 0.



            You want command substitution:



            #!/bin/bash

            server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
            if [[ "$server_count" -ne 3 ]]; then
            echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
            fi


            This will set server_count to the results of wc -l so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).



            Also the -ne check is being used here which is the correct check for integer comparison.



            On a side note you were using the POSIX shell test [ ... ] with a bash comparison operator ==. This will still work on many systems but beware when using [ ... ] you should use = and when using [[ ... ]] you can use either = or ==.






            share|improve this answer














            $? will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $? will be 0.



            You want command substitution:



            #!/bin/bash

            server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
            if [[ "$server_count" -ne 3 ]]; then
            echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
            fi


            This will set server_count to the results of wc -l so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).



            Also the -ne check is being used here which is the correct check for integer comparison.



            On a side note you were using the POSIX shell test [ ... ] with a bash comparison operator ==. This will still work on many systems but beware when using [ ... ] you should use = and when using [[ ... ]] you can use either = or ==.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 10 at 21:27

























            answered Aug 10 at 21:22









            Jesse_b

            10.5k22659




            10.5k22659











            • grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
              – glenn jackman
              Aug 13 at 13:49










            • And another grep after that: | grep -qxF 3 ... and you don't need the variable.
              – Kusalananda
              Aug 13 at 14:03
















            • grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
              – glenn jackman
              Aug 13 at 13:49










            • And another grep after that: | grep -qxF 3 ... and you don't need the variable.
              – Kusalananda
              Aug 13 at 14:03















            grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
            – glenn jackman
            Aug 13 at 13:49




            grep can count: server_count=$( nslookup servers | grep -c "Address: 10" )
            – glenn jackman
            Aug 13 at 13:49












            And another grep after that: | grep -qxF 3 ... and you don't need the variable.
            – Kusalananda
            Aug 13 at 14:03




            And another grep after that: | grep -qxF 3 ... and you don't need the variable.
            – Kusalananda
            Aug 13 at 14:03












            up vote
            0
            down vote













            As Jesse_b explained, your code does not work since you are looking at the return status of wc -l rather than the data returned from it.



            if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
            echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
            fi


            This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup, and if that count is not exactly three, it will send an email.



            With -c, grep will output the number of lines that matches the given pattern. The -q option to grep stops grep from generating output. Here, we're only interested in whether grep matches the 3 or not, and we check this by its return status. With -x we force a match across a whole line (i.e. we will match only 3 and not 30). The -F option makes grep do a string match instead of a regular expression match.






            share|improve this answer
























              up vote
              0
              down vote













              As Jesse_b explained, your code does not work since you are looking at the return status of wc -l rather than the data returned from it.



              if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
              echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
              fi


              This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup, and if that count is not exactly three, it will send an email.



              With -c, grep will output the number of lines that matches the given pattern. The -q option to grep stops grep from generating output. Here, we're only interested in whether grep matches the 3 or not, and we check this by its return status. With -x we force a match across a whole line (i.e. we will match only 3 and not 30). The -F option makes grep do a string match instead of a regular expression match.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                As Jesse_b explained, your code does not work since you are looking at the return status of wc -l rather than the data returned from it.



                if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
                echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
                fi


                This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup, and if that count is not exactly three, it will send an email.



                With -c, grep will output the number of lines that matches the given pattern. The -q option to grep stops grep from generating output. Here, we're only interested in whether grep matches the 3 or not, and we check this by its return status. With -x we force a match across a whole line (i.e. we will match only 3 and not 30). The -F option makes grep do a string match instead of a regular expression match.






                share|improve this answer












                As Jesse_b explained, your code does not work since you are looking at the return status of wc -l rather than the data returned from it.



                if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
                echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
                fi


                This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup, and if that count is not exactly three, it will send an email.



                With -c, grep will output the number of lines that matches the given pattern. The -q option to grep stops grep from generating output. Here, we're only interested in whether grep matches the 3 or not, and we check this by its return status. With -x we force a match across a whole line (i.e. we will match only 3 and not 30). The -F option makes grep do a string match instead of a regular expression match.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 13 at 14:34









                Kusalananda

                106k14209327




                106k14209327



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461905%2fwriting-a-script-to-check-if-the-output-of-a-command-is-3%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    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