在bash脚本亚行拉的奇怪行为 [英] strange behaviour of adb pull in bash script

查看:253
本文介绍了在bash脚本亚行拉的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写一个bash脚本来自动获取所有的WhatsApp的备份,但我不明白什么是错的。

 #!/斌/庆典亚行启动服务器
在$文件(ADB外壳LS / SD卡/ WhatsApp的/数据库)

    亚行拉/ SD卡/ WhatsApp的/数据库/文件$
DONE

输出是很奇怪的:

  $ ./script.sh
不existsdcard / WhatsApp的/数据库/ msgstore-2014-10-28.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-10-29.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-10-30.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-11-01.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-11-02.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-11-03.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore-2014-11-04.1.db.crypt7
不existsdcard / WhatsApp的/数据库/ msgstore.db.crypt7

由于亚行外壳LS / SD卡/ WhatsApp的/数据库的作品,应当有拉每个文件到当前工作目录中没有问题,但你可以看到,即使错误消息看起来不合式('不 existsdcard / WhatsApp的...)

我试过回声亚行拉/ SD卡/ WhatsApp的/数据库/文件$,而不是直接调用亚行的,如果我复制/粘贴到输出外壳的各单行,它的工作原理。
但是,任何其他尝试做到这一点自动失效。

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

编辑:
用它运行后

 #!/斌/ bash的-x

(感谢拉克什·古普塔建议)输出以下内容:

  +亚行启动服务器
亚行++外壳LS / SD卡/ WhatsApp的/数据库
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-10-28.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-10-28.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-10-29.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-10-29.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-10-30.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-10-30.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-11-01.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-11-01.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-11-02.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-11-02.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-11-03.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-11-03.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore-2014-11-04.1.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore-2014-11-04.1.db.crypt7'不存在
+在文件'$(ADB外壳LS / SD卡/ WhatsApp的/数据库)
亚行+ $拉'/ SD卡/ WhatsApp的/数据库/ msgstore.db.crypt7 \\ r'。
远程对象'/sdcard/WhatsApp/Dat​​abases/msgstore.db.crypt7'不存在

和确有在那很可能导致该行为每个文件名末尾的\\ r字符。

由于该链接我 https://stackoverflow.com/tags/bash/info ,其他的家伙,并加入TR -d'\\ R',在这种方式的脚本很好地运行

 #!/斌/庆典亚行启动服务器在$文件(ADB外壳LS / SD卡/ WhatsApp的/数据库| TR -d'\\ r')

    亚行拉/ SD卡/ WhatsApp的/数据库/ $文件。
DONE


解决方案

有可能是你的脚本特殊字符。尝试在你的脚本运行DOS2UNIX的。此外,尝试添加-n作为下检查语法

 #!/斌/ bash的-n

和运行脚本识别与语法的任何问题。

您也可以尝试

 #!/斌/ bash的-x

启用调试模式

I want to write a bash script to fetch all WhatsApp's backups automatically, but I cannot understand what's wrong

#!/bin/bash

adb start-server
for file in $(adb shell ls /sdcard/WhatsApp/Databases) 
do
    adb pull /sdcard/WhatsApp/Databases/$file
done

The output is very strange:

$ ./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

since "adb shell ls /sdcard/WhatsApp/Databases" works, there shall be no problem pulling each file into the current working directory, but as you can see, even the error message doesn't look well formed ("' does not existsdcard/WhatsApp...")

I tried echo "adb pull /sdcard/WhatsApp/Databases/$file" instead of directly calling adb, and if I copy/paste each single line of the output into the shell, it works. But any other attempt to do this automatically fails.

I feel like if I forgot some very basic concepts of bash scripting

Edit: After running it with

#!/bin/bash -x

(thanks to Rakesh Gupta suggestion) the output is the following:

+ 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

and indeed there is the \r character at the end of each file name that is likely to cause that behaviour.

Thanks to that other guy that linked me to https://stackoverflow.com/tags/bash/info, and adding "tr -d '\r'", in this way the script runs nicely

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

解决方案

There may be a special character in you script. Try running dos2unix on your script. Also, try adding -n as under to check the syntax

#!/bin/bash -n

and run the script to identify any issues with syntax.

You can also try

#!/bin/bash -x 

to enable the debug mode

这篇关于在bash脚本亚行拉的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆