1. 新建自己的shell文件,或者中windows上傳
例如: d://x.sh
- #! /system/bin/sh
- #最簡單的一個shell,沒什麼功能
- #需要注意的是必須是 #! /system/bin/sh開頭(有空格)
- #和一般linux不同,如果用#!/bin/sh就會找不到(因爲沒有)
- ls
複製代碼在cmd中用adb push d://x.sh /data/kenshin上傳shell
第一個參數是本地文件位置,第二個是遠程位置(也就是放在android中的位置),在根目錄的data目錄下,kenshin是我自己創建的一個文件夾. 如果失敗了,說是隻有隻讀權限的話,需要修改文件夾的權限.具體如下:
進入adb shell,使用mount命令查看掛了哪些塊.
- C:/Documents and Settings/Kenshintang>adb shell
- # mount
- mount
- rootfs / rootfs ro 0 0
- tmpfs /dev tmpfs rw,mode=755 0 0
- devpts /dev/pts devpts rw,mode=600 0 0
- proc /proc proc rw 0 0
- sysfs /sys sysfs rw 0 0
- tmpfs /sqlite_stmt_journals tmpfs rw,size=4096k 0 0
- /dev/block/mtdblock0 /system yaffs2 ro,noatime,nodiratime 0 0
- /dev/block/mtdblock1 /data yaffs2 rw,nosuid,nodev 0 0
- /dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0
複製代碼比如/dev/block/mtdblock0 /system就是隻讀的,/dev/block/mtdblock1 /data是讀寫的
修改讀寫權限用mount命令
例如 mount -t yaffs2 -o remount,rw,noatime,nodiratime /dev/mtdblock0 /system
-o選項用來描述設備或者檔案的掛接方式,常用的有
loop:用來把一個文件當成硬盤分區掛接上系統
ro:採用只讀方式掛接設備
rw:採用讀寫方式掛接設備
具體的用法可以google linux命令mout的用法.
上面那句話的意思就是讓/system文件夾有讀寫的權限
2. cd到shell所在的文件夾,查看x.sh的權限
一般上傳上去的話是沒有執行權限的,可以用ls -l查看
使用chmod 777 x.sh增加權限,具體可以google chmod的用法.
3. 執行 ./x.sh
4. 在程序中執行shell package com.kenshin.hellocommand;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloCommand extends Activity {
/** Called when the activity is first created. */
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv01);
try {
execCommand("./data/kenshin/x.sh");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void execCommand(String command) throws IOException {
// start the ls command running
//String[] args = new String[]{"sh", "-c", command};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); //這句話就是shell與高級語言間的調用
//如果有參數的話可以用另外一個被重載的exec方法
//實際上這樣執行時啓動了一個子進程,它沒有父進程的控制檯
//也就看不到輸出,所以我們需要用輸出流來得到shell執行後的輸出
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
//System.out.println(line);
sb.append(line);
sb.append('/n');
}
tv.setText(sb.toString());
//使用exec執行不會等執行成功以後才返回,它會立即返回
//所以在某些情況下是很要命的(比如複製文件的時候)
//使用wairFor()可以等待命令執行完成以後才返回
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
}
another way to create
- /**
- * 執行一個shell命令,並返回字符串值
- *
- * @param cmd
- * 命令名稱&參數組成的數組(例如:{"/system/bin/cat", "/proc/version"})
- * @param workdirectory
- * 命令執行路徑(例如:"system/bin/")
- * @return 執行結果組成的字符串
- * @throws IOException
- */
- public static synchronized String run(String[] cmd, String workdirectory)
- throws IOException {
- StringBuffer result = new StringBuffer();
- try {
- // 創建操作系統進程(也可以由Runtime.exec()啓動)
- // Runtime runtime = Runtime.getRuntime();
- // Process proc = runtime.exec(cmd);
- // InputStream inputstream = proc.getInputStream();
- ProcessBuilder builder = new ProcessBuilder(cmd);
- InputStream in = null;
- // 設置一個路徑(絕對路徑了就不一定需要)
- if (workdirectory != null) {
- // 設置工作目錄(同上)
- builder.directory(new File(workdirectory));
- // 合併標準錯誤和標準輸出
- builder.redirectErrorStream(true);
- // 啓動一個新進程
- Process process = builder.start();
- // 讀取進程標準輸出流
- in = process.getInputStream();
- byte[] re = new byte[1024];
- while (in.read(re) != -1) {
- result = result.append(new String(re));
- }
- }
- // 關閉輸入流
- if (in != null) {
- in.close();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return result.toString();
- }