我想編寫一個bash腳本來自動獲取所有的WhatsApp的備份,但我不明白什麼是錯奇怪的bash腳本亞行拉的行爲
#!/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
(感謝拉克什·古普塔建議)輸出以下後:
+ 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
這的確令人困惑,這是因爲回車。請參閱[bash標籤wiki](http://stackoverflow.com/tags/bash/info)上的步驟#1。 – 2014-11-04 23:24:30