shell | 您所在的位置:网站首页 › autojs延迟 › shell |
shell即Unix Shell,在类Unix系统提供与操作系统交互的一系列命令。很多程序可以用来执行shell命令,例如终端模拟器。 Auto.js Pro 9也内置了终端用于执行npm, node等命令,而本模块创建的Shell对象或者exec函数执行的shell命令,在非打包应用的情况下均支持node和npm命令,除非修改了PATH环境变量。 本模块主要提供了创建新的Shell对象的函数createShell用于持续地执行多条shell命令并监听结果,以及exec函数一次性执行一条shell命令并获取结果。 # 目录# 接口ExecutionResultShellShellOptions# 类型别名ExitResultPrivilegeTypeStandardOutputType# 函数InputTextSendKeySwipeTapcheckAccesscreateShellexecgetDefaultShellOptionsisRootAvailablesetDefaultShellOptions# 类型别名# ExitResultƬ ExitResult: string | number # PrivilegeTypeƬ PrivilegeType: "root" | "adb" # StandardOutputTypeƬ StandardOutputType: "stderr" | "stdout" # 函数# InputText▸ InputText(text): Promise 使用默认的shell示例执行input text命令,模拟输入文字。 默认的shell示例在首次使用Tap, Swipe, SendKey, InputText等操作时会使用默认Shell选项getDefaultShellOptions自动创建。若您想让这些操作使用adb权限或特定Shell选项,应当在执行这些函数前使用setDefaultShellOptions设置Shell选项。 这些操作函数以大写字母开头,表示其通常需要root或adb权限,否则只能在本应用内生效。 示例 "nodejs"; const { InputText } = require("shell"); async function main() { await InputText("Hello, World"); } main(); # 参数名称类型描述textstring要输入的文字,只支持ASCII字符,不支持中文# 返回值Promise # SendKey▸ SendKey(key): Promise 使用默认的shell示例执行input keyevent key命令,模拟发送按键key。 默认的shell示例在首次使用Tap, Swipe, SendKey, InputText等操作时会使用默认Shell选项getDefaultShellOptions自动创建。若您想让这些操作使用adb权限或特定Shell选项,应当在执行这些函数前使用setDefaultShellOptions设置Shell选项。 这些操作函数以大写字母开头,表示其通常需要root或adb权限,否则只能在本应用内生效。 示例 "nodejs"; const { SendKey, setDefaultShellOptions } = require("shell"); async function main() { setDefaultShellOptions({ adb: true, }); await SendKey("HOME"); } main(); # 参数名称类型描述keystringnumber# 返回值Promise # Swipe▸ Swipe(x1, y1, x2, y2, duration?): Promise 使用默认的shell示例执行input swipe x1 y1 x2 y2 duration命令,模拟从位置(x1, y1)滑动到位置(x2, y2)。 默认的shell示例在首次使用Tap, Swipe, SendKey, InputText等操作时会使用默认Shell选项getDefaultShellOptions自动创建。若您想让这些操作使用adb权限或特定Shell选项,应当在执行这些函数前使用setDefaultShellOptions设置Shell选项。 这些操作函数以大写字母开头,表示其通常需要root或adb权限,否则只能在本应用内生效。 示例 "nodejs"; const { Swipe } = require("shell"); async function main() { await Swipe(800, 100, 800, 1000); } main(); # 参数名称类型描述x1number-y1number-x2number-y2number-duration?number滑动时间,单位毫秒# 返回值Promise # Tap▸ Tap(x, y): Promise 使用默认的shell示例执行input tab x y命令,模拟点击位置(x, y)。 默认的shell示例在首次使用Tap, Swipe, SendKey, InputText等操作时会使用默认Shell选项getDefaultShellOptions自动创建。若您想让这些操作使用adb权限或特定Shell选项,应当在执行这些函数前使用setDefaultShellOptions设置Shell选项。 这些操作函数以大写字母开头,表示其通常需要root或adb权限,否则只能在本应用内生效。 示例 "nodejs"; const { Tap } = require("shell"); async function main() { await Tap(100, 100); } main(); # 参数名称类型xnumberynumber# 返回值Promise # checkAccess▸ checkAccess(type): Promise 检查是否有特定的特权,比如是否有root权限。 示例 "nodejs"; const { checkAccess } = require("shell"); async function main() { const hasRoot = await checkAccess("root"); const hasAdb = await checkAccess("adb"); console.log(`hasRoot: ${hasRoot}, hasAdb: ${hasAdb}`); } main(); # 参数名称类型描述typePrivilegeType特权类型,比如"root"或"adb"# 返回值Promise 是否有该类型的特权的Promise # createShell▸ createShell(options?): Shell 创建一个Shell实例。 我们通常用exec函数来一次性执行单条命令并获取结果,但如果有多条命令需要执行,用Shell对象的效率更高。这是因为无需每次执行都创建新的shell进程。 通过Shell对象我们也可监听到Shell的输出。 示例 "nodejs"; const { createShell } = require("shell"); const shell = createShell(); shell.on("line", (line) => { console.log(line); }); shell.exec("ls"); const id = $autojs.keepRunning(); shell.exit().then(() => $autojs.cancelKeepRunning(id)); # 参数名称类型描述options?ShellOptionsShell选项,将覆盖默认的Shell选项# 返回值Shell Shell实例 # exec▸ exec(cmd, options?): Promise 创建一个新的shell进程,并执行命令,异步返回结果。 示例 "nodejs"; const { exec, isRootAvailable } = require("shell"); async function main() { console.log(await exec("npm")); if (await isRootAvailable()) { console.log(await exec("ls /data", { root: true })); } } main(); # 参数名称类型描述cmdstring要执行的命令options?ShellOptionsShell选项,将覆盖默认的Shell选项# 返回值Promise 执行结果的Promise # getDefaultShellOptions▸ getDefaultShellOptions(): ShellOptions 获取默认的Shell选项。 参见 setDefaultShellOptions # 返回值ShellOptions # isRootAvailable▸ isRootAvailable(): Promise 检查设备是否已Root。需要注意的是,设备已Root不代表本应用已获得Root权限。 示例 "nodejs"; const { isRootAvailable } = require("shell"); async function main() { const rootAvailable = await isRootAvailable(); console.log(`rootAvailable: ${rootAvailable}`); } main(); # 返回值Promise # setDefaultShellOptions▸ setDefaultShellOptions(options): void 设置默认的Shell选项。这些选项包含是否使用Root权限、adb权限、环境变量等,会在创建新的Shell或RootAutomator实例时使用。 # 参数名称类型描述optionsShellOptionsShell选项# 返回值void |
CopyRight 2018-2019 实验室设备网 版权所有 |