Tooth uncle tutorial is easy to understand

The target

Run the shell script in the /data/local/ TMP directory using autojs

origin

To do a screen run script, you use a similar method,

For example, the screen uses ADB to call dex files to achieve the screen effect.

What they have in common is

  • Copy files to /data/local/ TMP
  • To change the permission, run the chmod 777 filename or chmod +x filename command

The environment

Phone: Mi 8

Android version: 10

Autojs version: 9.0.10

The code on

1. Copy files to 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=''}
Copy the code

Unfortunately, no permission, copy file failed,

So we need to increase our permissions, Shizuku can increase our permissions to adb level,

Please refer to the previous tutorial to activate Shizuku

With the permission promoted to adb level, we copied the files again

First, check if you have ADB permissions

let adbCheck = $shell.checkAccess("adb"); if (! AdbCheck) {console.log(" No ADB permission, please use Shizuku to activate autoJS ADB permission first "); exit(); } else {console.log(" adb permission "); }Copy the code

Copy the file

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

View file yashu.sh permissions

$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
'}
Copy the code

You can see that the permission is -rw-rw—-, there is no execute permission, next, we add execute permission

2. Add the execute permission
$shell.setDefaultOptions({ adb: true });
cmd = "chmod +x /data/local/tmp/yashu.sh";
r = $shell(cmd);
log(r);
// ShellResult{code=0, error='', result=''}
Copy the code

Code =0, 0 means no error, no error means that the command executed normally,

Check the file yashu.sh permissions again

$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
'}
Copy the code

You can see that the permission is -rwxrwx–x, and with the execute permission, we’re going to execute the shell script

3. Run the shell script

Shell script content

#!/bin/bash
echo "Uncle Teeth Tutorial is easy to understand"
author="Tooth uncle"
echo "Author: $author"
Copy the code

Execute the command of the shell script

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

note

The commands used to run the script are similar to those used to execute shell scripts, which is why I wrote this tutorial,

Easy to copy and paste later

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

The statement

This tutorial is intended for learning purposes only and is not intended for any other use