亚行的奇怪行为拉在bash脚本

我想编写一个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”起作用,所以将每个文件拖入当前工作目录是没有问题的,但是正如你所看到的,即使错误信息看起来并不正确 (“'不存在 / WhatsApp的……“)

我试着echo“adb pull / sdcard / WhatsApp / Databases / $ file”,而不是直接调用adb,如果我复制/粘贴每一行输出到shell,它的工作原理。 但是任何其他的尝试自动失败。

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

编辑:运行后

 #!/bin/bash -x 

(感谢Rakesh Gupta的build议)输出结果如下:

 + 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 

Solutions Collecting From Web of "亚行的奇怪行为拉在bash脚本"

你的脚本中可能有一个特殊的字符。 尝试在脚本上运行dos2unix。 另外,尝试添加-n作为下面检查语法

 #!/bin/bash -n 

并运行该脚本来识别语法的任何问题。

你也可以试试

 #!/bin/bash -x 

启用调试模式