How to find all interactive commands in a shell script using java?












0















I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read, designed to capture user input and I think that it is the only one.



I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))).



Here is my Validator class:



import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class Validator {

Script script;

String interactiveCommands = {"read"};

public Validator(Script script) {
this.script = script;
}

public void validateInteractiveCommands() {
for (String string : interactiveCommands) {
streamService(string);
}
}

private void streamService(String string) {
try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
stream.filter(lines -> lines.startsWith(string))
.forEach(this::printFound);
} catch (IOException e) {
e.printStackTrace();
}
}

private void printFound(String string) {
System.out.println("Found an interactive command: " + string);
}

}


But in this case I only capture read when it is at the beggining of a line.
And in this case:



if true ; then
read a
fi


it fails to capture read because of 4 spaces.
Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?










share|improve this question



























    0















    I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read, designed to capture user input and I think that it is the only one.



    I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))).



    Here is my Validator class:



    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.stream.Stream;

    public class Validator {

    Script script;

    String interactiveCommands = {"read"};

    public Validator(Script script) {
    this.script = script;
    }

    public void validateInteractiveCommands() {
    for (String string : interactiveCommands) {
    streamService(string);
    }
    }

    private void streamService(String string) {
    try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
    stream.filter(lines -> lines.startsWith(string))
    .forEach(this::printFound);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private void printFound(String string) {
    System.out.println("Found an interactive command: " + string);
    }

    }


    But in this case I only capture read when it is at the beggining of a line.
    And in this case:



    if true ; then
    read a
    fi


    it fails to capture read because of 4 spaces.
    Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?










    share|improve this question

























      0












      0








      0








      I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read, designed to capture user input and I think that it is the only one.



      I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))).



      Here is my Validator class:



      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.util.stream.Stream;

      public class Validator {

      Script script;

      String interactiveCommands = {"read"};

      public Validator(Script script) {
      this.script = script;
      }

      public void validateInteractiveCommands() {
      for (String string : interactiveCommands) {
      streamService(string);
      }
      }

      private void streamService(String string) {
      try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
      stream.filter(lines -> lines.startsWith(string))
      .forEach(this::printFound);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }

      private void printFound(String string) {
      System.out.println("Found an interactive command: " + string);
      }

      }


      But in this case I only capture read when it is at the beggining of a line.
      And in this case:



      if true ; then
      read a
      fi


      it fails to capture read because of 4 spaces.
      Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?










      share|improve this question














      I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read, designed to capture user input and I think that it is the only one.



      I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))).



      Here is my Validator class:



      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.util.stream.Stream;

      public class Validator {

      Script script;

      String interactiveCommands = {"read"};

      public Validator(Script script) {
      this.script = script;
      }

      public void validateInteractiveCommands() {
      for (String string : interactiveCommands) {
      streamService(string);
      }
      }

      private void streamService(String string) {
      try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
      stream.filter(lines -> lines.startsWith(string))
      .forEach(this::printFound);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }

      private void printFound(String string) {
      System.out.println("Found an interactive command: " + string);
      }

      }


      But in this case I only capture read when it is at the beggining of a line.
      And in this case:



      if true ; then
      read a
      fi


      it fails to capture read because of 4 spaces.
      Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?







      java string bash lambda find






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 7:28









      CrystalzordCrystalzord

      559




      559
























          1 Answer
          1






          active

          oldest

          votes


















          3















          "I know that there is a command read, designed to capture user input and I think that it is the only one."




          In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:



            rm -i *
          cat > file


          Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:



            #!/bin/sh
          echo running "$@"
          "$@"





          share|improve this answer
























          • Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

            – Crystalzord
            Nov 22 '18 at 8:09






          • 1





            Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

            – Stephen C
            Nov 22 '18 at 9:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53425844%2fhow-to-find-all-interactive-commands-in-a-shell-script-using-java%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









          3















          "I know that there is a command read, designed to capture user input and I think that it is the only one."




          In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:



            rm -i *
          cat > file


          Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:



            #!/bin/sh
          echo running "$@"
          "$@"





          share|improve this answer
























          • Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

            – Crystalzord
            Nov 22 '18 at 8:09






          • 1





            Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

            – Stephen C
            Nov 22 '18 at 9:07
















          3















          "I know that there is a command read, designed to capture user input and I think that it is the only one."




          In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:



            rm -i *
          cat > file


          Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:



            #!/bin/sh
          echo running "$@"
          "$@"





          share|improve this answer
























          • Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

            – Crystalzord
            Nov 22 '18 at 8:09






          • 1





            Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

            – Stephen C
            Nov 22 '18 at 9:07














          3












          3








          3








          "I know that there is a command read, designed to capture user input and I think that it is the only one."




          In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:



            rm -i *
          cat > file


          Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:



            #!/bin/sh
          echo running "$@"
          "$@"





          share|improve this answer














          "I know that there is a command read, designed to capture user input and I think that it is the only one."




          In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:



            rm -i *
          cat > file


          Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:



            #!/bin/sh
          echo running "$@"
          "$@"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 7:45









          Stephen CStephen C

          515k69563920




          515k69563920













          • Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

            – Crystalzord
            Nov 22 '18 at 8:09






          • 1





            Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

            – Stephen C
            Nov 22 '18 at 9:07



















          • Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

            – Crystalzord
            Nov 22 '18 at 8:09






          • 1





            Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

            – Stephen C
            Nov 22 '18 at 9:07

















          Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

          – Crystalzord
          Nov 22 '18 at 8:09





          Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?

          – Crystalzord
          Nov 22 '18 at 8:09




          1




          1





          Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

          – Stephen C
          Nov 22 '18 at 9:07





          Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.

          – Stephen C
          Nov 22 '18 at 9:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53425844%2fhow-to-find-all-interactive-commands-in-a-shell-script-using-java%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'