使用运行方式在ADB Shell中复制文件 [英] Copying files in ADB shell with run-as

查看:718
本文介绍了使用运行方式在ADB Shell中复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以编写脚本来使用运行方式从ADB Shell复制文件?

Is there a way to write a script that will copy files from an ADB shell using run-as?

我知道要在adb中复制的唯一方法外壳使用 cat源> dest (编辑:现代android版本具有 cp 命令,这使此问题不再必要),但我只能引用大于号深入签名一级-因此我的脚本可以将其传递给adb shell,但不能传递给adb shell运行身份。

The only way I know of to copy in the adb shell is using cat source > dest (edit: modern android versions have the cp command, which makes this question unnecessary), but I am only able to quote the greater-than sign one level deep - so my script can pass it to adb shell, but not to adb shell run-as.

例如,这有效:

adb shell猫源&dest

但是不是:

adb shell运行方式为 cat source> dest

也不要:

adb shell运行时猫源\>目标

我什至尝试创建一个小脚本并将其上传到设备上,但是我似乎无法从adb shell运行该脚本-它告诉我没有权限。我也无法更改脚本。

I even tried created a small script and uploading it to the device, but I can't seem to run the script from the adb shell - it tells me "permission denied". I can't chmod the script, either.

我要这样做的原因是将文件复制到应用程序的专用存储区中-具体来说,我正在使用脚本来修改共享的首选项并放回修改后的首选项。但是,只有应用程序本身或root才能写入我想要的文件。

The reason I want to do this is to copy a file into an app's private storage area - specifically, I am using a script to modify shared preferences and put the modified preferences back. Only the app itself or root can write to the file I want, however.

这种情况下的用例是将文件复制到设备上受保护的位置,而不是检索它;对于检索,此问题中已经有很好的答案。。 p>

The use case in this scenario is coping a file to a protected location on the device, not retrieving it; for retrieving, there are already good answers in this question.

推荐答案

OP尝试将以下3个命令(他在交互式shell会话中一个接一个地执行没有问题)组合在一起。单个非交互式命令:

The OP tried to combine the following 3 commands (that he had no problem executing one after another in the interactive shell session) into a single non-interactive command:

adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml

为简单起见,我们从交互式 adb shell 会话。如果我们只是尝试将最后两个命令合并为一行:

For simplicity let's start from within an interactive adb shell session. If we just try to combine the last two commands into a single line:

run-as com.example.app cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml

由于shell重定向的工作原理,该方法不起作用-仅该命令的 cat /sdcard/temp_prefs.xml 部分将与 com.example.app <$ c运行$ c> UID

This would not work because of how shell redirection works - only the cat /sdcard/temp_prefs.xml part of the command would be run with com.example.app UID

许多人知道 将围绕重定向的命令部分放在引号中:

Many people "know" to put the part of the command around redirection into quotes:

run-as com.example.app "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"

这不起作用,因为运行方式命令不够智能,无法分析整个命令。它期望将可执行文件作为下一个参数。正确的方法是使用 sh 代替:

This does not work because the run-as command is not smart enough to parse the whole command. It expects an executable as the next parameter. The proper way to do it would be to use sh instead:

run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"

那么我们可以将 adb shell 放在命令前面并完成它吗?不必要。通过从PC运行命令,您还可以添加另一个本地Shell及其解析器。特定的转义要求取决于您的操作系统。在Linux或OSX中(如果您的命令尚未包含任何'),可以很容易地用单引号将整个命令括起来,如下所示:

So can we just prepend adb shell to the command and be done with it? Not necessarily. By running the command from your PC you also add another local shell and its parser. Specific escape requirements would depend on your OS. In Linux or OSX (if your command does not already contain any ') it is easy to single-quote the whole command like so:

adb shell 'run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"'

但是有时使用带(-或更少)引号的替代解决方案会更容易: / p>

But sometimes it is just easier to use an alternative solutions with (-out or less) quotes:

adb shell run-as com.example.app cp /sdcard/temp_prefs.xml shared_prefs/com.example.app_preferences.xml

或者如果您的设备没有 cp 命令:

Or if your device does not have the cp command:

adb shell run-as com.example.app dd if=/sdcard/temp_prefs.xml of=shared_prefs/com.example.app_preferences.xml

还要注意我是如何使用 shared_prefs / com的。 example.app_preferences.xml 而不是完整的 /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml -通常内体运行方式命令中的e,当前目录是软件包的 HOME 目录。

Also notice how I used shared_prefs/com.example.app_preferences.xml instead of full /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml - normally inside of run-as command your current directory is the HOME dir of your package.

这篇关于使用运行方式在ADB Shell中复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆