adb中的奇怪行为拉入bash脚本

时间:2014-11-04 23:12:55

标签: 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 ...“)

我尝试回复“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 \ n字符可能会导致该行为。

感谢那个将我链接到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

1 个答案:

答案 0 :(得分:2)

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

#!/bin/bash -n

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

您也可以尝试

#!/bin/bash -x 

启用调试模式