执行 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

 

转载于:https://www.cnblogs.com/codeskilla/p/4961582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值