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
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.
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.
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.")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With