android - adb pull 在 bash 脚本中的奇怪行为

标签 android linux bash shell adb

<分区>

我想写一个 bash 脚本来自动获取所有 WhatsApp 的备份,但我不明白哪里出了问题

#!/bin/bash

adb start-server
for file in $(adb shell ls /sdcard/WhatsApp/Databases) 
do
    adb pull /sdcard/WhatsApp/Databases/$file
done

输出很奇怪:

$ ./script.sh
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore.db.crypt7

由于“adb shell ls/sdcard/WhatsApp/Databases”有效,将每个文件拉入当前工作目录应该没有问题,但如您所见,即使错误消息看起来格式不正确(“'不existsdcard/WhatsApp...")

我试过 echo "adb pull/sdcard/WhatsApp/Databases/$file"而不是直接调用 adb,如果我将输出的每一行复制/粘贴到 shell 中,它就可以工作。 但是任何其他尝试自动失败。

我觉得如果我忘记了 bash 脚本的一些非常基本的概念

编辑: 运行后

#!/bin/bash -x

(感谢 Rakesh Gupta 的建议)输出如下:

+ adb start-server
++ adb shell ls /sdcard/WhatsApp/Databases
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7' does not     exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore.db.crypt7' does not exist

事实上,每个文件名末尾的\r 字符可能会导致该行为。

感谢将我链接到 https://stackoverflow.com/tags/bash/info 的那个人, 并添加 "tr -d '\r'", 这样脚本运行得很好

#!/bin/bash 

adb start-server

for file in $(adb shell ls /sdcard/WhatsApp/Databases | tr -d '\r')
do
    adb pull /sdcard/WhatsApp/Databases/$file .
done

最佳答案

您的脚本中可能有特殊字符。尝试在您的脚本上运行 dos2unix。另外,尝试添加 -n 来检查语法

#!/bin/bash -n

并运行脚本以识别任何语法问题。

你也可以试试

#!/bin/bash -x 

启用 Debug模式

关于android - adb pull 在 bash 脚本中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26746853/

相关文章:

c++ - 启动浏览器的 Posixy 方式?

linux - 获取打开特定文件的应用程序列表

regex - 使用 grep 匹配固定字符串 + 数字 0-10

android - 手动清除 Android ViewModel?

Android:颜色代码

linux - 终端上的命令行管道语法

Bash-awk-大文件每一行的并行选择过程

bash - 在具有相同分隔符的多个实例的行上使用 cut - unix

android - 旋转屏幕后找不到微调器

android - 为什么 URLConnection 会在 6 分钟以上而不是 5 秒后超时?