美文网首页
shell脚本

shell脚本

作者: 牙叔教程 | 来源:发表于2021-10-15 19:44 被阅读0次

牙叔教程 简单易懂

目标

用autojs执行shell脚本, 脚本所在目录为 /data/local/tmp

缘起

做息屏运行脚本, 要用到类似的方法,

比如息屏使用adb去调用dex文件, 来达到息屏效果,

两者相同的地方是

  • 都要复制文件到 /data/local/tmp文件夹下
  • 都要修改权限, 命令为 chmod 777 filename 或者 chmod +x filename

环境

手机: Mi 8

Android版本: 10

Autojs版本: 9.0.10

代码讲解

1. 复制文件到 data/local/tmp
cmd = "cp /sdcard/yashu.sh /data/local/tmp/yashu.sh";
r = shell(cmd);
log(r);
// ShellResult{code=1, error='cp: /data/local/tmp/yashu.sh: Permission denied
', result=''}

非常不幸, 没有权限, 复制文件失败,

所以我们需要要提高权限, Shizuku可以提升我们的权限到adb级别,

Shizuku的使用请查阅上一篇教程, 激活Shizuku

将权限提升为adb级别之后, 我们重新复制文件

首先, 检查有没有adb权限

let adbCheck = $shell.checkAccess("adb");
if (!adbCheck) {
  console.log("没有adb权限, 请先使用shizuku激活autojs的adb权限");
  exit();
} else {
  console.log("已有adb权限");
}

复制文件

$shell.setDefaultOptions({ adb: true });
cmd = "cp /sdcard/yashu.sh /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result=''}

查看文件yashu.sh权限

$shell.setDefaultOptions({ adb: true });
cmd = "ls -al /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result='-rw-rw---- 1 shell shell 84 2021-10-15 17:43 /data/local/tmp/yashu.sh
'}

可以看到权限是-rw-rw----, 没有执行权限, 接下来, 我们就添加执行权限

2. 添加执行权限
$shell.setDefaultOptions({ adb: true });
cmd = "chmod +x /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result=''}

code=0, 0表示没有发生错误, 没有错误就意味着, 命令正常执行,

再次查看文件yashu.sh权限

$shell.setDefaultOptions({ adb: true });
cmd = "ls -al /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result='-rwxrwx--x 1 shell shell 84 2021-10-15 17:46 /data/local/tmp/yashu.sh
'}

可以看到权限是-rwxrwx--x, 有了执行权限, 接下来, 我们就执行这个shell脚本

3. 执行shell脚本

shell脚本内容

#!/bin/bash
echo "牙叔教程 简单易懂"
author="牙叔"
echo "作者: $author"

执行shell脚本的命令

$shell.setDefaultOptions({ adb: true });
cmd = "sh /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result='牙叔教程 简单易懂
// 作者: 牙叔
// '}

备注

息屏运行脚本使用到的命令与执行shell脚本类似, 这也是我写这篇教程的原因,

方便以后复制黏贴

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • chapter 11. 构建基本脚本

    创建shell脚本 shell脚本第一行为指定具体shell来运行该脚本,可以指定shell(待验证) echo ...

  • PySparkSQL脚本模板

    PySpark模板分为shell脚本和python脚本两部分,通过shell脚本提交spark任务。 shell脚...

网友评论

      本文标题:shell脚本

      本文链接:https://www.haomeiwen.com/subject/tdomoltx.html