2013-11-25 17 views
3

执行shell我知道如何在外壳从Android应用程序执行命令Android在前面的命令的情况下,从应用

shell.exec("ls /"); 

以及如何从中读取

new BufferedReader(new InputStreamReader(_process.getInputStream())).readLine(); 

响应,但我有一个shell运行后需要额外用户输入的应用程序。我想知道如何发送额外的输入到相同的shell命令。

例如:

cp /abc/ /a/abc/ 

而且可以说,该命令要求用户通过输入查询额外Y确认覆盖,该怎么做?

回答

0

尝试

Process process = Runtime.getRuntime().exec(commandLine); 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
1

尝试使用此方法发送多个命令通过shell.This致力于通过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(); 
      } 
+0

嗨。感谢您的重播,但是这些命令会立即被一一发送。但我想要的是发送命令,比如'cp',然后读取输出,一旦输出包含'你想重写文件吗?我发送响应'Y',这是可能的,我能够读取和分析响应,但我不能发送响应,以便在问题提供的背景下理解它,如果我使用'shell.exec(“Y”);'它会给出错误。 (我知道你可以使用标志覆盖所有,但我必须每次询问用户)。 – user2570174

+0

我认为你所要求的是不可能的,因为这些功能只能用于命令而不能用于用户输入。 – krishna

+0

好吧,也许有可能运行隐藏在后台的终端外壳,并发送输入,就好像用户在真实shell中键入它一样,这可能吗? – user2570174

相关问题