2014-11-04 52 views
3

我想編寫一個bash腳本來自動獲取所有的WhatsApp的備份,但我不明白什麼是錯奇怪的b​​ash腳本亞行拉的行爲

#!/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 
+2

這的確令人困惑,這是因爲回車。請參閱[bash標籤wiki](http://stackoverflow.com/tags/bash/info)上的步驟#1。 – 2014-11-04 23:24:30

回答

2

有可能是一個特殊的字符在你的腳本。嘗試在腳本上運行dos2unix。另外,嘗試添加-n以檢查語法

#!/bin/bash -n 

並運行腳本以識別語法的任何問題。

您也可以嘗試

#!/bin/bash -x 

啓用調試模式

+1

關閉,但額外的字符在adb的輸出中,而不是腳本。有人顯然認爲這將是一個窗口框。 – 2014-11-05 01:54:42