Check if a give port is included in output, if it is run another command












1














I have a commandb:
printf '%s' "${PORTS[0]}"; printf ',%s' "${PORTS[@]:1}"



That lists ports from another command that output of commandb looks like:



2200,800,8000


These ports are stored in a file name res.txt



I want to check if commandb has a specific port of 8000, if it does, I want to run a commandc, otherwise do nothing.



How can I achieve this in bash?










share|improve this question



























    1














    I have a commandb:
    printf '%s' "${PORTS[0]}"; printf ',%s' "${PORTS[@]:1}"



    That lists ports from another command that output of commandb looks like:



    2200,800,8000


    These ports are stored in a file name res.txt



    I want to check if commandb has a specific port of 8000, if it does, I want to run a commandc, otherwise do nothing.



    How can I achieve this in bash?










    share|improve this question

























      1












      1








      1







      I have a commandb:
      printf '%s' "${PORTS[0]}"; printf ',%s' "${PORTS[@]:1}"



      That lists ports from another command that output of commandb looks like:



      2200,800,8000


      These ports are stored in a file name res.txt



      I want to check if commandb has a specific port of 8000, if it does, I want to run a commandc, otherwise do nothing.



      How can I achieve this in bash?










      share|improve this question













      I have a commandb:
      printf '%s' "${PORTS[0]}"; printf ',%s' "${PORTS[@]:1}"



      That lists ports from another command that output of commandb looks like:



      2200,800,8000


      These ports are stored in a file name res.txt



      I want to check if commandb has a specific port of 8000, if it does, I want to run a commandc, otherwise do nothing.



      How can I achieve this in bash?







      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 20 '18 at 16:45









      john jones

      1124




      1124






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use grep -w to specify a word boundary:



          $ grep -qw '8000' res.txt && echo 'hello world'
          hello world


          The -q option just prevents grep from printing the results so you can simply act on the exit code. Without the -w option you would get a successful result from 80 as it is contained within 800 and 8000:



          $ grep -q '80' res.txt && echo 'hello world'; echo $?
          hello world
          0
          $ grep -qw '80' res.txt && echo 'hello world'; echo $?
          1


          In order to check for the existence of multiple ports you can do the following:



          if grep -qw -e '8000' -e '80' res.txt; then
          do something
          fi





          share|improve this answer























          • can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
            – john jones
            Dec 20 '18 at 17:02










          • Do you want to know if both exist or if one or the other exists?
            – Jesse_b
            Dec 20 '18 at 17:02










          • one or other please.
            – john jones
            Dec 20 '18 at 17:03










          • @johnjones: Does my update help?
            – Jesse_b
            Dec 20 '18 at 17:23






          • 1




            you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
            – ilkkachu
            Dec 20 '18 at 17:28











          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%2f490152%2fcheck-if-a-give-port-is-included-in-output-if-it-is-run-another-command%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use grep -w to specify a word boundary:



          $ grep -qw '8000' res.txt && echo 'hello world'
          hello world


          The -q option just prevents grep from printing the results so you can simply act on the exit code. Without the -w option you would get a successful result from 80 as it is contained within 800 and 8000:



          $ grep -q '80' res.txt && echo 'hello world'; echo $?
          hello world
          0
          $ grep -qw '80' res.txt && echo 'hello world'; echo $?
          1


          In order to check for the existence of multiple ports you can do the following:



          if grep -qw -e '8000' -e '80' res.txt; then
          do something
          fi





          share|improve this answer























          • can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
            – john jones
            Dec 20 '18 at 17:02










          • Do you want to know if both exist or if one or the other exists?
            – Jesse_b
            Dec 20 '18 at 17:02










          • one or other please.
            – john jones
            Dec 20 '18 at 17:03










          • @johnjones: Does my update help?
            – Jesse_b
            Dec 20 '18 at 17:23






          • 1




            you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
            – ilkkachu
            Dec 20 '18 at 17:28
















          1














          You can use grep -w to specify a word boundary:



          $ grep -qw '8000' res.txt && echo 'hello world'
          hello world


          The -q option just prevents grep from printing the results so you can simply act on the exit code. Without the -w option you would get a successful result from 80 as it is contained within 800 and 8000:



          $ grep -q '80' res.txt && echo 'hello world'; echo $?
          hello world
          0
          $ grep -qw '80' res.txt && echo 'hello world'; echo $?
          1


          In order to check for the existence of multiple ports you can do the following:



          if grep -qw -e '8000' -e '80' res.txt; then
          do something
          fi





          share|improve this answer























          • can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
            – john jones
            Dec 20 '18 at 17:02










          • Do you want to know if both exist or if one or the other exists?
            – Jesse_b
            Dec 20 '18 at 17:02










          • one or other please.
            – john jones
            Dec 20 '18 at 17:03










          • @johnjones: Does my update help?
            – Jesse_b
            Dec 20 '18 at 17:23






          • 1




            you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
            – ilkkachu
            Dec 20 '18 at 17:28














          1












          1








          1






          You can use grep -w to specify a word boundary:



          $ grep -qw '8000' res.txt && echo 'hello world'
          hello world


          The -q option just prevents grep from printing the results so you can simply act on the exit code. Without the -w option you would get a successful result from 80 as it is contained within 800 and 8000:



          $ grep -q '80' res.txt && echo 'hello world'; echo $?
          hello world
          0
          $ grep -qw '80' res.txt && echo 'hello world'; echo $?
          1


          In order to check for the existence of multiple ports you can do the following:



          if grep -qw -e '8000' -e '80' res.txt; then
          do something
          fi





          share|improve this answer














          You can use grep -w to specify a word boundary:



          $ grep -qw '8000' res.txt && echo 'hello world'
          hello world


          The -q option just prevents grep from printing the results so you can simply act on the exit code. Without the -w option you would get a successful result from 80 as it is contained within 800 and 8000:



          $ grep -q '80' res.txt && echo 'hello world'; echo $?
          hello world
          0
          $ grep -qw '80' res.txt && echo 'hello world'; echo $?
          1


          In order to check for the existence of multiple ports you can do the following:



          if grep -qw -e '8000' -e '80' res.txt; then
          do something
          fi






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 20 '18 at 17:32

























          answered Dec 20 '18 at 16:55









          Jesse_b

          11.9k23064




          11.9k23064












          • can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
            – john jones
            Dec 20 '18 at 17:02










          • Do you want to know if both exist or if one or the other exists?
            – Jesse_b
            Dec 20 '18 at 17:02










          • one or other please.
            – john jones
            Dec 20 '18 at 17:03










          • @johnjones: Does my update help?
            – Jesse_b
            Dec 20 '18 at 17:23






          • 1




            you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
            – ilkkachu
            Dec 20 '18 at 17:28


















          • can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
            – john jones
            Dec 20 '18 at 17:02










          • Do you want to know if both exist or if one or the other exists?
            – Jesse_b
            Dec 20 '18 at 17:02










          • one or other please.
            – john jones
            Dec 20 '18 at 17:03










          • @johnjones: Does my update help?
            – Jesse_b
            Dec 20 '18 at 17:23






          • 1




            you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
            – ilkkachu
            Dec 20 '18 at 17:28
















          can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
          – john jones
          Dec 20 '18 at 17:02




          can you check for multiple ports like 80 and 8080 with -qw '80' || '8080' ?
          – john jones
          Dec 20 '18 at 17:02












          Do you want to know if both exist or if one or the other exists?
          – Jesse_b
          Dec 20 '18 at 17:02




          Do you want to know if both exist or if one or the other exists?
          – Jesse_b
          Dec 20 '18 at 17:02












          one or other please.
          – john jones
          Dec 20 '18 at 17:03




          one or other please.
          – john jones
          Dec 20 '18 at 17:03












          @johnjones: Does my update help?
          – Jesse_b
          Dec 20 '18 at 17:23




          @johnjones: Does my update help?
          – Jesse_b
          Dec 20 '18 at 17:23




          1




          1




          you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
          – ilkkachu
          Dec 20 '18 at 17:28




          you should be able to give grep multiple patterns to match against, e.g. echo '123,456,789' | grep -w -e 123 -e 111 should report a match and print the line
          – ilkkachu
          Dec 20 '18 at 17:28


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f490152%2fcheck-if-a-give-port-is-included-in-output-if-it-is-run-another-command%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

          Morgemoulin

          Scott Moir

          Souastre