Android shell 腳本

2018-09-03

cat file1 > file2????//讀取/復制file1為file2

echo "string" > file? ? //覆蓋寫入string到file

echo "string" >> file? ? //追加寫入string到file

//注意引號里面的內(nèi)容string要replaceAll("\\n", "\\\\n")血公,replaceAll("\"", "\\\\\"");

下面是一個執(zhí)行多條命令的方法

public static CommandResultexecCommand(String[] commands, boolean isRoot) {

????????CommandResult commandResult =new CommandResult();

? ? ? ? if (commands ==null || commands.length ==0)return commandResult;

? ? ? ? Process process =null;

? ? ? ? DataOutputStream os =null;

? ? ? ? BufferedReader successResult =null;

? ? ? ? BufferedReader errorResult =null;

? ? ? ? StringBuilder successMsg =null;

? ? ? ? StringBuilder errorMsg =null;

? ? ? ? try {

????????????process = Runtime.getRuntime().exec(isRoot ?COMMAND_SU :COMMAND_SH);

? ? ? ? ? ? os =new DataOutputStream(process.getOutputStream());

? ? ? ? ? ? for (String command : commands) {

????????????????if (command !=null) {

????????????????os.write(command.getBytes());

? ? ? ? ? ? ? ? ? ? os.writeBytes(COMMAND_LINE_END);

? ? ? ? ? ? ? ? ? ? os.flush();

? ? ? ? ? ? ? ? }

????????}

????????os.writeBytes(COMMAND_EXIT);

? ? ? ? ? ? os.flush();

? ? ? ? ? ? commandResult.result = process.waitFor();

? ? ? ? ? ? //獲取錯誤信息

? ? ? ? ? ? successMsg =new StringBuilder();

? ? ? ? ? ? errorMsg =new StringBuilder();

? ? ? ? ? ? successResult =new BufferedReader(new InputStreamReader(process.getInputStream()));

? ? ? ? ? ? errorResult =new BufferedReader(new InputStreamReader(process.getErrorStream()));

? ? ? ? ? ? String s;

? ? ? ? ? ? while ((s = successResult.readLine()) !=null) successMsg.append(s);

? ? ? ? ? ? while ((s = errorResult.readLine()) !=null) errorMsg.append(s);

? ? ? ? ? ? commandResult.successMsg = successMsg.toString();

? ? ? ? ? ? commandResult.errorMsg = errorMsg.toString();

? ? ? ? }catch (IOException e) {

????????????String errmsg = e.getMessage();

? ? ? ? ? ? if (errmsg !=null) {

????????????????Log.e(TAG, errmsg);

? ? ? ? ? ? }else {

????????????????e.printStackTrace();

? ? ? ? ? ? }

????????}catch (Exception e) {

????????????String errmsg = e.getMessage();

? ? ? ? ? ? if (errmsg !=null) {

????????????????Log.e(TAG, errmsg);

? ? ? ? ? ? }else {

????????????????e.printStackTrace();

? ? ? ? ? ? }

????????}finally {

????????try {

????????????????if (os !=null) os.close();

? ? ? ? ? ? ? ? if (successResult !=null) successResult.close();

? ? ? ? ? ? ? ? if (errorResult !=null) errorResult.close();

? ? ? ? ? ? }catch (IOException e) {

????????????????String errmsg = e.getMessage();

? ? ? ? ? ? ? ? if (errmsg !=null) {

????????????????????Log.e(TAG, errmsg);

? ? ? ? ? ? ? ? }else {

????????????????????e.printStackTrace();

? ? ? ? ? ? ? ? }

????????}

????????if (process !=null) process.destroy();

?????}

?????return commandResult;

}

修改Packages.xml文件權(quán)限

String path ="/storage/emulated/0/temp.xml";

CommandExecution.execCommand("cat /data/system/packages.xml > " + path, true);

File file =new File(path);

if (!file.exists()) {

????Log.d("File", path +"不存在");

}else {

????StringBuffer sb =new StringBuffer();

? ? try {

????????InputStream is =new FileInputStream(file);

? ? ? ? if (is !=null) {

????????????InputStreamReader isr =new InputStreamReader(is);

? ? ? ? ? ? BufferedReader br =new BufferedReader(isr);

? ? ? ? ? ? String line;

? ? ? ? ? ? while ((line = br.readLine()) !=null) {

????????????????sb.append(line);

? ? ? ? ? ? ? ? sb.append("\n");

? ? ? ? ? ? }

????????????CommandExecution.execCommand("echo \"" +

????????????sb.toString().replaceAll("granted=\"false\"", "granted=\"true\"")

????????????????????????????????.replaceAll("\\n", "\\\\n")

????????????????????????????????.replaceAll("\"", "\\\\\"") +

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "\" > /data/system/packages.xml", true);

? ? ? ? ? ? Log.i("setPermission", "Success");

? ? ? ? }

}catch (FileNotFoundException e) {

????????Log.d("FileNotFoundException", file.getAbsolutePath() +" doesn't found\n" + e.getMessage());

? ? }catch (IOException e) {

????????Log.d("File", "read exception: " + e.getMessage());

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末狗唉,一起剝皮案震驚了整個濱河市柳恐,隨后出現(xiàn)的幾起案子雷滋,更是在濱河造成了極大的恐慌,老刑警劉巖章喉,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件维贺,死亡現(xiàn)場離奇詭異告唆,居然都是意外死亡,警方通過查閱死者的電腦和手機簇秒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門鱼喉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人趋观,你說我怎么就攤上這事扛禽。” “怎么了皱坛?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵编曼,是天一觀的道長。 經(jīng)常有香客問我剩辟,道長掐场,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任贩猎,我火速辦了婚禮熊户,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吭服。我一直安慰自己嚷堡,他們只是感情好,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布艇棕。 她就那樣靜靜地躺著蝌戒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪欠肾。 梳的紋絲不亂的頭發(fā)上瓶颠,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音刺桃,去河邊找鬼粹淋。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的桃移。 我是一名探鬼主播屋匕,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼借杰!你這毒婦竟也來了过吻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蔗衡,失蹤者是張志新(化名)和其女友劉穎纤虽,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绞惦,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡逼纸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了济蝉。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片杰刽。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖王滤,靈堂內(nèi)的尸體忽然破棺而出贺嫂,到底是詐尸還是另有隱情,我是刑警寧澤雁乡,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布第喳,位于F島的核電站,受9級特大地震影響蔗怠,放射性物質(zhì)發(fā)生泄漏墩弯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一寞射、第九天 我趴在偏房一處隱蔽的房頂上張望渔工。 院中可真熱鬧,春花似錦桥温、人聲如沸引矩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旺韭。三九已至,卻和暖如春掏觉,著一層夾襖步出監(jiān)牢的瞬間区端,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工澳腹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留织盼,地道東北人杨何。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像沥邻,于是被迫代替她去往敵國和親危虱。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容