<分区>
我想写一个 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