Bash是去除而命令? [英] Bash is removing commands in while?

查看:104
本文介绍了Bash是去除而命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白,为什么发生这种情况时,bash似乎删除命令在,而循环运行时。
我有以下脚本:

 亚行外壳LS /富/栏|而读文件

    文件夹=/富/酒吧/ $文件
    亚行拉$文件夹下载
DONE

当我seperatly运行命令,它的伟大工程。但闻一运行就这样,我得到下面的输出:

 '不existfoo /酒吧/ filesdfsdf.sdf

在/富/酒吧每个文件。

所以我想给我的命令回字符串,我改成回声亚行拉$文件夹中下载,得到了以下的输出:

 下载/富/酒吧/ fileasdfs.sd​​f

每一行。看来,bash所做的事情与字符串这里取代一些东西...
如果我使用 ,所以不会发生bash的更换,我得到正确的输出。

发生了什么吗?

编辑:我想通了,不是,而直接打破,其亚行外壳命令什么相呼应CRLF在每一行结束。如果我使用

 亚行外壳LS /富/栏| TR -d'\\ 015'|而读文件

    文件夹=/富/酒吧/ $文件
    亚行拉$文件夹下载
DONE

它的作品!
但是,为什么是这样的bash过敏CRLF?和为什么它删除comnmands?


解决方案

我不知道你所说的为什么而不允许CR?的意思。正确的问题是什么是读做CR?。除非另有说明,使用CR参数分裂。看在你的bash-手册变量 IFS ,并有一看 -d 选项来读取。

例如:

  BOS:$回声foo的酒吧gazonk |同时读取X;做回声$ X; DONE
富巴gazonk

在这里,CR作为(默认)分隔符。现在,假设你要读的每一个字拆,那么你可以使用 -d(设置SPC作为分隔符):

  $回声foo的酒吧gazonk你好123 |而读-d,X;做回声$ X; DONE

酒吧
gazonk
你好

这里的背面是最后一个参数(123)不输出,因为它不与一个定界符结尾。分隔符看作抓住一切留下它,直到但不包括下一个分隔符。因为123不以分隔符结尾,最后一个分隔符是你好和123之间,并且 - 这样 - 什么是抓住是你好,因为它有一个分隔符,左,以它的权利。

I really dont understand why this is happening, bash seems to remove commands while running in a while loop. I have the following script:

adb shell ls /foo/bar | while read file 
do
    folder="/foo/bar/$file"                                                                                                                                                                                                                                                    
    adb pull $folder download
done

when i run the command seperatly, it works great. But wen i run it like this, i get the following output:

' does not existfoo/bar/filesdfsdf.sdf

for every file in /foo/bar.

SO i tried to give me the command back as string, i changed to echo "adb pull $folder download" and got the following output:

 download/foo/bar/fileasdfs.sdf

for each line. It seems that bash does something with the string and replaces some stuff here... If i use ', so no bash replacement would occur, i get the correct output.

whats happening here?

EDIT: I figured out that not while is directly broken, its the adb shell command what echoes a CRLF on every line end. if i use

adb shell ls /foo/bar | tr -d '\015' | while read file 
do
    folder="/foo/bar/$file"                                                                                                                                                                                                                                                    
    adb pull $folder download
done

it works! But why is bash so allergic to CRLF? And why its deletes comnmands?

解决方案

I don't know what you mean by "Why does while not allow CR?". The correct question is "What does read do with CR?". Unless stating otherwise, read uses CR for parameter splitting. Look in your bash-manual for the variable IFS, and have a look at the -d option to read.

Example:

bos:$ echo foo bar gazonk | while read x ; do echo $x ; done
foo bar gazonk

Here, the CR acts as (default) delimiter for read. Now, say you want to split the read at every word, then you would use -d " " (setting SPC as delimiter):

$ echo foo bar gazonk hello 123 | while read -d " " x ; do echo $x ; done
foo
bar
gazonk
hello

The backside here is that the last parameter (123) is not output because it does not end with a delimiter. Think of the delimiter as grabbing everything LEFT of it, up to but not including the next delimiter. Since "123" does not end with a delimiter, the last delimiter is between "hello" and "123", and - thus - what's grabbed is "hello" because it has a delimiter to left and to right of it.

这篇关于Bash是去除而命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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