以编程方式执行 shell 命令

发布于 2024-12-29 09:14:18 字数 198 浏览 2 评论 0原文

我想通过程序执行这个shell命令。我该怎么做呢?

cd C:\android-sdk\platform-tools
adb shell
su
mount -t rfs -o remount,rw /dev/block/stl9 /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk

I want to execute this shell commands by program. How can I do it?

cd C:\android-sdk\platform-tools
adb shell
su
mount -t rfs -o remount,rw /dev/block/stl9 /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城缀染半城烟沙 2025-01-05 09:14:18

我们可以使用 Runtime 类来执行 shell 命令。

Runtime.getRuntime().exec("ls");

上面的代码将为给定命令 ls 创建一个本机进程,并将返回与 Process 对象相同的进程。

有关它的更多详细信息查看此处

We can execute shell comands by using Runtime class.

Runtime.getRuntime().exec("ls");

The above piece of code will create a native process for given command ls, will return same process as a Process object.

For more details about it Check here

呢古 2025-01-05 09:14:18

您应该将此处使用的确切语法写入 .bat 文件中,然后执行它。

You Should write the exact syntax you used here in a .bat file, and then just execute it.

雨后咖啡店 2025-01-05 09:14:18

看来您在 Microsoft 工作站上,因此考虑使用批处理会给您带来以下结果:

第一种方法:留在您的工作站上并发送有用的命令

cd C:\android-sdk\platform-tools
adb shell "su -c 'mount -o rw,remount /system'"
adb shell "su -c 'cp /sdcard/MyApp.apk /system/app/MyApp.apk'"
adb shell "su -c 'mount -o ro,remount /system'"

唯一的事情是您将启动和关闭 3 个 shell,但事实并非如此并发出。

第二种方法:留在您的工作站上,在 sdcard 上发送一个 sh 脚本,并

cd C:\android-sdk\platform-tools
adb push myscript.sh /sdcard/
adb shell "su -c 'sh /sdcard/myscript.sh'"

使用包含以下内容的“myscript.sh”执行它:

#!/system/bin/sh
mount -o rw,remount /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
mount -o ro,remount /system

请记住,在 Microsoft 工作站上创建的 Android shell 脚本具有 CRLF 行结尾!
在类 UNIX 系统上,您只需要使用 LF 来结束行!

It seems you are on a Microsoft station so considering using batch would give you this :

1st method : Stay on your station and send usefull commands

cd C:\android-sdk\platform-tools
adb shell "su -c 'mount -o rw,remount /system'"
adb shell "su -c 'cp /sdcard/MyApp.apk /system/app/MyApp.apk'"
adb shell "su -c 'mount -o ro,remount /system'"

The only thing is you will launch and close 3 shells but its not really and issue.

2nd method : Stay on your station send a sh script on sdcard and execute it

cd C:\android-sdk\platform-tools
adb push myscript.sh /sdcard/
adb shell "su -c 'sh /sdcard/myscript.sh'"

with "myscript.sh" containing :

#!/system/bin/sh
mount -o rw,remount /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
mount -o ro,remount /system

Remember that Android shell scripts created on Microsoft station have CRLF line ending !
You need to get LF only ending your lines on UNIX like systems !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文