android 运行shell 脚本文件或shell命令

这篇具有很好参考价值的文章主要介绍了android 运行shell 脚本文件或shell命令。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

android 运行shell 脚本文件或shell命令
一.运行shell脚本文件
1.test.sh文件内容
#!/bin/bash
echo "I am a script"
ps
2.将shell文件拷贝到Android设备目录
3.执行脚本文件 Runtime.getRuntime().exec("sh /sdcard/lilei/test.sh");
注:应用需要有存储访问权限,如果shell文件中有文件访问请用绝对路径,否则访问不到
二.运行shell命令
调用Runtime.getRuntime().exec(String cmdarray[]) ,这里传递命令数组或者Runtime.getRuntime().exec(String command)直接执行命令。

注:有些shell命令可以在 adb shell 中运行,但是通过应用Runtime执行命令会失败(即使当前已经是uid system权限)当前测试系统版本Android11.
例如 adb shell中可以执行执命令: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");
系统日志会报错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 权限。

可执行adb root后再执行:
setenforce 0 (临时禁用掉SELinux)
getenforce (得到结果为Permissive)
上述命令执行完之后,代码就可以执行安装命令了,基本可以确认是SELinux造成的权限问题,需要通过正规的方式来解决权限问题。

-----------------具体实现如下--------------------------------
/**
* 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;
}
}文章来源地址https://www.toymoban.com/news/detail-838709.html

到了这里,关于android 运行shell 脚本文件或shell命令的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • PowerShell 命令窗口执行 pnpm 命令报错 无法加载文件 pnpm.ps1,因为在此系统上禁止运行脚本

    在 PowerShell 命令行窗口使用 pnpm run dev 启动 vue3-element-admin 报错: pnpm : 无法加载文件 C:UsersyoulaiAppDataRoamingnpmpnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 abo ut_Execution_Policies。 以管理员身份运行 Windows PowerShell 执行

    2024年02月07日
    浏览(54)
  • nodejs脚本中执行shell命令

    Node.js v8.x 中文文档: child_process - 子进程 Node.js中使用内置的 child_process 模块来执行shell命令。该模块提供了 exec 、 execFile 、 spawn 等方法来启动子进程并执行命令 exec 方法是将整个命令输出缓存到内存中,当执行 完成后一次性 返回,所以适合执行 较小 的命令 exec 方法的 回调

    2024年01月21日
    浏览(39)
  • 【Linux命令-shell】虚拟机中创建shell脚本、查看当前路径、执行脚本

    目录 一、创建shell脚本 二、查看当前的路径 三、执行脚本 一、创建shell脚本 shell脚本的特点 提前将可执行的命令语句写入一个文件中 顺序执行 解释器逐行解释代码 常见的脚本有:shell、python、PHP...... 注:用什么解释器就是什么脚本 编写shell脚本: 步骤: 1、新建文件 2、

    2024年02月05日
    浏览(47)
  • shell脚本-批量主机执行命令(expect)

    上次连接多台服务器使用ssh-keygen,24机器去连接22、25,所以存在.ssh/authorized_keys 1.如果有.ssh/authorized_keys该文件则先删除 1.expect命令含义 expect是一种脚本语言,它能够代替人工实现与终端的交互,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互

    2024年02月13日
    浏览(45)
  • Linux shell编程学习笔记44:编写一个脚本,将md5sum命令执行结果保存到变量中,进而比较两个文件内容是否相同

    在  Linux shell编程学习笔记42:md5sum https://blog.csdn.net/Purpleendurer/article/details/137125672?spm=1001.2014.3001.5501 中,我们提到编写一个在Linux系统下比较两个文件内容是否相同的脚本。 基本思路是: 其中有两个难点: 1.文件的md5值的获取 2.md5值的比较 对于第1个难点,我们的解决办法是

    2024年04月10日
    浏览(56)
  • shell脚本ssh远程执行命令给变量赋值的问题

    从A机器通过SSH方式到B机器,并执行相关的命令。命令中包含变量及变量的赋值。 代码如下,意思是,ssh到192.111.111.27这台机器,cd到 / 根目录下,并执行ls命令,如果ls出来的结果不为空,则执行echo命令。可以肯定的是 / 根目录下是有内容的。 可以看到当执行到 echo 命令的时

    2024年02月12日
    浏览(39)
  • Linux 查询正在运行的shell脚本命令

    1.查看当前运行的所有进程。 ps -A 2.如果太多了找不到,看的眼花,可以加条件 grep是分组 查看正在运行的shell脚本的进程shell脚本就是 sh ps -ef |grep  sh 如图下面就是查询出来的所有sh脚本,看第三列就是脚本的进程UID,直接杀死UID就行    3.杀死进程UID kill  4491 如图 直接杀

    2024年02月12日
    浏览(37)
  • 【Linux】Shell脚本中获取命令运行的结果

    写shell脚本的时候,常需要将一个命令的运行结果做为参数传递给另外一个命令,除了我们熟知的管道 | 和args,我们也可以通过获取命令的运行结果。 执行结果: 来点复杂的应用: 再比如: 😉 运行结果: 把反引号``换成$()即可 反引号不支持嵌套,而 $ 支持嵌套。 举个例

    2024年02月11日
    浏览(28)
  • 【Linux Shell】6. echo 命令

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

    2024年01月22日
    浏览(38)
  • postgresql|数据库|批量执行SQL脚本文件的shell脚本

    对于数据库的维护而言,肯定是有SQL脚本的执行,例如,某个项目需要更新,那么,可能会有很多的SQL脚本需要执行,SQL脚本可能会包含有建表,插入数据,索引建立,约束建立,主外键建立等等内容。 那么,几个SQL脚本可能无所谓,navicat或者psql命令行 简简单单的就导入了

    2024年02月01日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包