zoukankan      html  css  js  c++  java
  • 执行 bash/sh 命令

    网址:“https://www.webmasterworld.com/linux/3613813.htm”

    我的测试代码:

     1         Button btnShellCommand01 = (Button)findViewById(R.id.btnShellCommand01);
     2         btnShellCommand01.setOnClickListener(new View.OnClickListener()
     3         {
     4             @Override
     5             public void onClick(View v)
     6             {
     7                 // https://www.webmasterworld.com/linux/3613813.htm
     8                 File file = new File("/bin");
     9                 MsgAppendLn02("file : "+file.toString());
    10                 if (file.exists())
    11                     MsgAppendLn02(file.toString() + " exists");
    12                 else
    13                     MsgAppendLn02(file.toString() + " not exists");
    14                 //Log.i("zzz", file);
    15                 Process process = null;
    16                 MsgAppendLn02("01");
    17                 try
    18                 {
    19                     //process = Runtime.getRuntime().exec("/bin/bash", null, file);// no such file or directory
    20                     process = Runtime.getRuntime().exec("/system/bin/sh", null, file);
    21                 }
    22                 catch(Exception ex)
    23                 {
    24                     StringWriter sw = new StringWriter();
    25                     PrintWriter pw = new PrintWriter(sw);
    26                     ex.printStackTrace(pw);
    27                     String strStackTrace = sw.toString();
    28                     MsgAppendLn02(strStackTrace);
    29                 }
    30                 MsgAppendLn02("02");
    31                 if (process != null)
    32                 {
    33                     MsgAppendLn02("03");
    34                     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    35                     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);
    36                     
    37                     out.println("cd ..");
    38                     out.println("pwd");
    39                     out.println("exit");
    40                     try
    41                     {
    42                         MsgAppendLn02("04");
    43                         String line;
    44                         while((line = in.readLine()) != null)
    45                         {
    46                             MsgAppendLn02(line);
    47                         }
    48                         process.waitFor();
    49                         in.close();
    50                         out.close();
    51                         process.destroy();
    52                         
    53                         MsgAppendLn02("05");
    54                     }
    55                     catch(Exception ex)
    56                     {
    57                         StringWriter sw = new StringWriter();
    58                         PrintWriter pw = new PrintWriter(sw);
    59                         ex.printStackTrace(pw);
    60                         String strStackTrace = sw.toString();
    61                         MsgAppendLn02(strStackTrace);
    62                     }
    63                 } // if
    64                 
    65                 Toast.makeText(MainActivity.this, "Shell Command 01 finish .", Toast.LENGTH_SHORT).show();
    66             }
    67         }); // btnShellCommand01

    网页内容保存于:百度云 CodeSkill33 --> “全部文件 > 来自__网页 > Java_Linux” --> Java_bash_command_Linux__Work

    X

  • 相关阅读:
    canvas beginPath()的初步理解
    高效取余运算(n-1)&hash原理探讨
    EntityUtils.toString(entity)处理字符集问题解决
    python计算不规则图形面积算法
    VMware与 Device/Credential Guard 不兼容,解决办法及心得
    Java爬取51job保存到MySQL并进行分析
    纯C语言实现循环双向链表创建,插入和删除
    纯C语言实现顺序队列
    纯C语言实现链队
    纯C语言实现链栈
  • 原文地址:https://www.cnblogs.com/codeskilla/p/4961582.html
Copyright © 2011-2022 走看看