我知道如何从 android 应用程序在 shell 中执行命令
shell.exec("ls /");
以及如何从中读取响应
new BufferedReader(new InputStreamReader(_process.getInputStream())).readLine();
但我有一个 shell 应用程序,运行后需要额外的用户输入。我想知道如何向同一个 shell 命令发送额外的输入。
例如:
cp /abc/ /a/abc/
假设该命令要求用户通过输入额外的 Y
来确认覆盖,该怎么做?
最佳答案
尝试通过 shell 发送多个命令。这可以通过 JSCH 客户端进行 ssh
Channel channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
ps.println("mkdir folder");
ps.println("dir");
//give commands to be executed inside println.and can have any no of commands sent.
ps.close();
InputStream in=channel.getInputStream();
byte[] bt=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(bt, 0, 1024);
if(i<0)
break;
String str=new String(bt, 0, i);
//displays the output of the command executed.
System.out.print(str);
}
if(channel.isClosed())
{
break;
}
Thread.sleep(1000);
channel.disconnect();
session.disconnect();
}
关于Android 在来自应用程序的先前命令的上下文中执行 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185446/