国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

android 運行shell 腳本文件或shell命令

這篇具有很好參考價值的文章主要介紹了android 運行shell 腳本文件或shell命令。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

android 運行shell 腳本文件或shell命令
一.運行shell腳本文件
1.test.sh文件內(nèi)容
#!/bin/bash
echo "I am a script"
ps
2.將shell文件拷貝到Android設備目錄
3.執(zhí)行腳本文件 Runtime.getRuntime().exec("sh /sdcard/lilei/test.sh");
注:應用需要有存儲訪問權限,如果shell文件中有文件訪問請用絕對路徑,否則訪問不到
二.運行shell命令
調(diào)用Runtime.getRuntime().exec(String cmdarray[]) ,這里傳遞命令數(shù)組或者Runtime.getRuntime().exec(String command)直接執(zhí)行命令。

注:有些shell命令可以在 adb shell 中運行,但是通過應用Runtime執(zhí)行命令會失?。词巩斍耙呀?jīng)是uid system權限)當前測試系統(tǒng)版本Android11.
例如 adb shell中可以執(zhí)行執(zhí)命令:pm install -r -t /data/local/tmp/test-debug.apk 安裝應用


但是通過代碼運行無法安裝:Process p = Runtime.getRuntime().exec("pm install -r -t /data/local/tmp/test-debug.apk");
系統(tǒng)日志會報錯01-09 15:35:45.548 18925 18925 W cmd : type=1400 audit(0.0:580): avc: denied { read } for name="test-debug.apk" dev="vdb" ino=1130534 scontext=u:r:system_app:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=0

缺少什么權限: ? ? { read }權限,
誰缺少權限: ? ? ? ?scontext=u:r:system_app:s0?
對哪個文件缺少權限: tcontext=u:object_r:shell_data_file:s0
什么類型的文件: ?tclass=file?
完整的意思: scontext=u:r:system_app:s0進程對shell_data_file:s0類型的file缺少read 權限。

可執(zhí)行adb root后再執(zhí)行:
setenforce 0 (臨時禁用掉SELinux)
getenforce (得到結果為Permissive)
上述命令執(zhí)行完之后,代碼就可以執(zhí)行安裝命令了,基本可以確認是SELinux造成的權限問題,需要通過正規(guī)的方式來解決權限問題。

-----------------具體實現(xiàn)如下--------------------------------
/**
* date: 04/23/2020.
* author: lilei.
*/
public class RuntimeUtil {
private static final String TAG = AppConstants.APP_TAG + "RuntimeUtil ";
private static final String SAVE_LOG_DIR_PATH = AppConstants.TSP_DIR + "/log/";

/**
* 測試運行shell 腳本文件
* @return
*/
public static String testRunShell() {
Log.d(TAG, "testRunShell 11");
String cmd = "ps";
try {
java.lang.Process p = Runtime.getRuntime().exec("sh /sdcard/lilei/test.sh");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
int index = 0;
while ((line = in.readLine()) != null) {
Log.d(TAG, "testRunShell 22 index:"+index +" line:"+line);
index++;
}
} catch (IOException e) {
Log.d(TAG, "testRunShell err=" + e.toString());
}

Log.d(TAG, "testRunShell 33");
return null;
}
/**
* getRetrieveLog from cmd adb logcat.
* for log test.
*
* @param timeMillis timeMillis.
* @param line if line is 0,set recent 5 second time.
*/
public static void getRetrieveLog(long timeMillis, String line) {
String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(timeMillis, 5000);
Log.d(TAG, "getRetrieveLog() begin retrieveTime:" + retrieveTime
+ " line:" + line);
try {
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-t");
if ("0".equals(line)) {
cmdLine.add(retrieveTime);
} else {
cmdLine.add(line);
}
Log.d(TAG, "getRetrieveLog() cmdLine:" + cmdLine.toString());

Process process = Runtime.getRuntime().exec(
cmdLine.toArray(new String[cmdLine.size()]));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

String str = null;
while ((str = bufferedReader.readLine()) != null) {
Log.d(TAG, "getRetrieveLog() str:" + str);
}
if (str == null) {
Log.d(TAG, "getRetrieveLog() end!!-- is null --");
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* getRetrieveLogToFile from cmd adb logcat.
*
* @param timeMillis current time.
* @param recentSeconds recent seconds.
* @param fileName file name.
* @return Full log path.
*/
public static String getRetrieveLogToFile(long timeMillis, int recentSeconds, String fileName) {
String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(
timeMillis, recentSeconds * 1000);
Log.d(TAG, "getRetrieveLogToFile() begin UTF-8 timeMillis:" + timeMillis
+ " recentSeconds:" + recentSeconds + " begin retrieveTime:" + retrieveTime);
BufferedWriter bufferedWriter = null;
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-t");
cmdLine.add(retrieveTime);

Log.d(TAG, "getRetrieveLog() cmdLine:" + cmdLine.toString());
Process process = null; //capture log.
String fullLogPath = null;
try {
//create a new path.
File dirFile = new File(SAVE_LOG_DIR_PATH);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
process = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));

fullLogPath = SAVE_LOG_DIR_PATH + fileName;
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fullLogPath), StandardCharsets.UTF_8));
int len = 0;
byte[] buf = new byte[1024];
String str = null;
while ((str = bufferedReader.readLine()) != null) {
//Start reading the log, one line at a time.
//Log.d(TAG, "getRetrieveLogToFile() str:" + str);
bufferedWriter.write(str + "\r\n");
}

} catch (IOException e1) {
Log.d(TAG, "getRetrieveLogToFile() error:" + e1);
e1.printStackTrace();
} catch (Exception e) {
Log.d(TAG, "getRetrieveLogToFile() error:" + e);
e.printStackTrace();
} finally {
if (null != bufferedWriter) {
try {
bufferedWriter.close();
bufferedWriter = null;
} catch (IOException e) {
Log.e(TAG, "getRetrieveLogToFile() error:" + e);
}
}
}
Log.d(TAG, "getRetrieveLogToFile() end!!");
return fullLogPath;
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-838709.html

到了這里,關于android 運行shell 腳本文件或shell命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • PowerShell 命令窗口執(zhí)行 pnpm 命令報錯 無法加載文件 pnpm.ps1,因為在此系統(tǒng)上禁止運行腳本

    PowerShell 命令窗口執(zhí)行 pnpm 命令報錯 無法加載文件 pnpm.ps1,因為在此系統(tǒng)上禁止運行腳本

    在 PowerShell 命令行窗口使用 pnpm run dev 啟動 vue3-element-admin 報錯: pnpm : 無法加載文件 C:UsersyoulaiAppDataRoamingnpmpnpm.ps1,因為在此系統(tǒng)上禁止運行腳本。有關詳細信息,請參閱 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 abo ut_Execution_Policies。 以管理員身份運行 Windows PowerShell 執(zhí)行

    2024年02月07日
    瀏覽(32)
  • nodejs腳本中執(zhí)行shell命令

    Node.js v8.x 中文文檔: child_process - 子進程 Node.js中使用內(nèi)置的 child_process 模塊來執(zhí)行shell命令。該模塊提供了 exec 、 execFile 、 spawn 等方法來啟動子進程并執(zhí)行命令 exec 方法是將整個命令輸出緩存到內(nèi)存中,當執(zhí)行 完成后一次性 返回,所以適合執(zhí)行 較小 的命令 exec 方法的 回調(diào)

    2024年01月21日
    瀏覽(23)
  • 【Linux命令-shell】虛擬機中創(chuàng)建shell腳本、查看當前路徑、執(zhí)行腳本

    目錄 一、創(chuàng)建shell腳本 二、查看當前的路徑 三、執(zhí)行腳本 一、創(chuàng)建shell腳本 shell腳本的特點 提前將可執(zhí)行的命令語句寫入一個文件中 順序執(zhí)行 解釋器逐行解釋代碼 常見的腳本有:shell、python、PHP...... 注:用什么解釋器就是什么腳本 編寫shell腳本: 步驟: 1、新建文件 2、

    2024年02月05日
    瀏覽(92)
  • shell腳本-批量主機執(zhí)行命令(expect)

    上次連接多臺服務器使用ssh-keygen,24機器去連接22、25,所以存在.ssh/authorized_keys 1.如果有.ssh/authorized_keys該文件則先刪除 1.expect命令含義 expect是一種腳本語言,它能夠代替人工實現(xiàn)與終端的交互,主要應用于執(zhí)行命令和程序時,系統(tǒng)以交互形式要求輸入指定字符串,實現(xiàn)交互

    2024年02月13日
    瀏覽(17)
  • Linux shell編程學習筆記44:編寫一個腳本,將md5sum命令執(zhí)行結果保存到變量中,進而比較兩個文件內(nèi)容是否相同

    Linux shell編程學習筆記44:編寫一個腳本,將md5sum命令執(zhí)行結果保存到變量中,進而比較兩個文件內(nèi)容是否相同

    在? Linux shell編程學習筆記42:md5sum https://blog.csdn.net/Purpleendurer/article/details/137125672?spm=1001.2014.3001.5501 中,我們提到編寫一個在Linux系統(tǒng)下比較兩個文件內(nèi)容是否相同的腳本。 基本思路是: 其中有兩個難點: 1.文件的md5值的獲取 2.md5值的比較 對于第1個難點,我們的解決辦法是

    2024年04月10日
    瀏覽(27)
  • shell腳本ssh遠程執(zhí)行命令給變量賦值的問題

    shell腳本ssh遠程執(zhí)行命令給變量賦值的問題

    從A機器通過SSH方式到B機器,并執(zhí)行相關的命令。命令中包含變量及變量的賦值。 代碼如下,意思是,ssh到192.111.111.27這臺機器,cd到 / 根目錄下,并執(zhí)行l(wèi)s命令,如果ls出來的結果不為空,則執(zhí)行echo命令??梢钥隙ǖ氖?/ 根目錄下是有內(nèi)容的。 可以看到當執(zhí)行到 echo 命令的時

    2024年02月12日
    瀏覽(25)
  • Linux 查詢正在運行的shell腳本命令

    Linux 查詢正在運行的shell腳本命令

    1.查看當前運行的所有進程。 ps -A 2.如果太多了找不到,看的眼花,可以加條件 grep是分組?查看正在運行的shell腳本的進程shell腳本就是 sh ps -ef |grep? sh 如圖下面就是查詢出來的所有sh腳本,看第三列就是腳本的進程UID,直接殺死UID就行 ? ?3.殺死進程UID kill? 4491 如圖 直接殺

    2024年02月12日
    瀏覽(18)
  • 【Linux】Shell腳本中獲取命令運行的結果

    【Linux】Shell腳本中獲取命令運行的結果

    寫shell腳本的時候,常需要將一個命令的運行結果做為參數(shù)傳遞給另外一個命令,除了我們熟知的管道 | 和args,我們也可以通過獲取命令的運行結果。 執(zhí)行結果: 來點復雜的應用: 再比如: ?? 運行結果: 把反引號``換成$()即可 反引號不支持嵌套,而 $ 支持嵌套。 舉個例

    2024年02月11日
    瀏覽(32)
  • 【Linux Shell】6. echo 命令

    【Linux Shell】6. echo 命令

    Shell 的 echo 指令用于字符串的輸出。命令格式:

    2024年01月22日
    瀏覽(26)
  • postgresql|數(shù)據(jù)庫|批量執(zhí)行SQL腳本文件的shell腳本

    postgresql|數(shù)據(jù)庫|批量執(zhí)行SQL腳本文件的shell腳本

    對于數(shù)據(jù)庫的維護而言,肯定是有SQL腳本的執(zhí)行,例如,某個項目需要更新,那么,可能會有很多的SQL腳本需要執(zhí)行,SQL腳本可能會包含有建表,插入數(shù)據(jù),索引建立,約束建立,主外鍵建立等等內(nèi)容。 那么,幾個SQL腳本可能無所謂,navicat或者psql命令行 簡簡單單的就導入了

    2024年02月01日
    瀏覽(85)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包