DEV Community

fuzi8849
fuzi8849

Posted on

Shell脚本记录

目标
用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 javascript 代码解读复制代码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权限
ini 代码解读复制代码let adbCheck = $shell.checkAccess("adb");
if (!adbCheck) {
console.log("没有adb权限, 请先使用shizuku激活autojs的adb权限");
exit();
} else {
console.log("已有adb权限");
}

复制文件
ini 代码解读复制代码$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权限
ini 代码解读复制代码$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----, 没有执行权限, 接下来, 我们就添加执行权限

  1. 添加执行权限 javascript 代码解读复制代码$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权限
ini 代码解读复制代码$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脚本

  1. 执行shell脚本 shell脚本内容 javascript 代码解读复制代码#!/bin/bash echo "fuzi教程 简单易懂" author="fuzi" echo "作者: $author"

执行shell脚本的命令
ini 代码解读复制代码$shell.setDefaultOptions({ adb: true });
cmd = "sh /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result='fuzi教程 简单易懂
// '}

备注
息屏运行脚本使用到的命令与执行shell脚本类似, 这也是我写这篇教程的原因,
方便以后复制黏贴

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay