Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command with prompt YES input

 public static String executeCommand(String command) {
    StringBuffer sb = new StringBuffer();
    Process p;
    try {
      p = Runtime.getRuntime().exec(command);
      p.waitFor();
     }   BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = "";
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sb.toString();
}

given code is working fine to execute any command but I have a command which want YES/NO as input. How I can give the input to command for further execution?

ex.

executeCommand("pio app data-delete model");

output-
[INFO] [App$] Data of the following app (default channel only) will be deleted. Are you sure?
[INFO] [App$]     App Name: twittermodeling
[INFO] [App$]       App ID: 14
[INFO] [App$]  Description: None
Enter 'YES' to proceed:

So How I can give YES to them for further execution.

Thanks

like image 356
Kishore Avatar asked Oct 05 '15 12:10

Kishore


People also ask

How do I use YES in CMD?

Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.

How do you automate input in command prompt?

To avoid this manual input, use the command "echo Y | del /P " in your batch script to answer the prompt. You can try this echo command to pass input (ex: answer for username and password prompts) to your console application, when it is invoked through batch script.

How do I add yn to Python?

def ask_yesno(question): """ Helper to get yes / no answer from user. """ yes = {'yes', 'y'} no = {'no', 'n'} # pylint: disable=invalid-name done = False print(question) while not done: choice = input(). lower() if choice in yes: return True elif choice in no: return False else: print("Please respond by yes or no.")


1 Answers

If you really need to pass "YES" to an unix command, you could prefix it by echo YES |. echo YES | pio app data-delete model should force deletion.

In the context of runtime.exec() thought the pipe can't be evaluated correctly, refer to this post for more information.

However, the first thing you should do is check if the pio command does not have a "force" flag, usually -f, which would omit user interactions.

like image 170
Aaron Avatar answered Sep 17 '22 12:09

Aaron