首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >亚行引入bash脚本的奇怪行为

亚行引入bash脚本的奇怪行为
EN

Stack Overflow用户
提问于 2014-11-04 23:12:54
回答 1查看 1.4K关注 0票数 3

我想写一个bash脚本来自动获取WhatsApp的所有备份,但我不明白出了什么问题

代码语言:javascript
运行
复制
#!/bin/bash

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

输出非常奇怪:

代码语言:javascript
运行
复制
$ ./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...")’)

我尝试了回显"adb拉/sdcard/WhatsApp/Databases/$file“,而不是直接调用亚行,如果我将输出的每一行复制/粘贴到shell中,它就能工作。但是任何其他的尝试都会自动失败。

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

编辑:在运行它之后

代码语言:javascript
运行
复制
#!/bin/bash -x

(感谢Rakesh Gupta的建议)产出如下:

代码语言:javascript
运行
复制
+ 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'",这个脚本运行得很好

代码语言:javascript
运行
复制
#!/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-04 23:24:56

在你的剧本里可能有一个特殊的角色。尝试在脚本上运行dos2unix。另外,尝试按下面的方式添加-n以检查语法

代码语言:javascript
运行
复制
#!/bin/bash -n

并运行脚本以标识语法方面的任何问题。

你也可以试试

代码语言:javascript
运行
复制
#!/bin/bash -x 

启用调试模式

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26746853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档