简体   繁体   English

循环遍历 docker bash 命令的输出

[英]Looping through output of docker bash command

I am trying to loop through a docker bash command like this: sudo docker exec -it my-container bash -c "cd sites && ls" > output.txt which gives me expected output, when I loop through it as: cat output.txt | while read line do echo "abcd${line}def"; done我试图循环这样一个 docker bash 命令: sudo docker exec -it my-container bash -c "cd sites && ls" > output.txt这给了我预期的输出,当我循环它时: cat output.txt | while read line do echo "abcd${line}def"; done cat output.txt | while read line do echo "abcd${line}def"; done cat output.txt | while read line do echo "abcd${line}def"; done it gives me output as: cat output.txt | while read line do echo "abcd${line}def"; done它给了我输出:

defdfile1
defdfile2

in short it is overlapping after file name If I do all this without the docker command like only ls on my host it works fine, how to resolve this issue?简而言之,它在文件名后重叠如果我在没有 docker 命令的情况下执行所有这些操作,例如在我的主机上仅使用 ls 它可以正常工作,如何解决此问题?

When using the -t flag docker outputs to standard out with added carriage return character.当使用-t标志 docker 输出到标准输出时,添加回车符。 It shows as ^M with cat -vte output.txt .它用cat -vte output.txt显示为^M ( -v , --show-nonprinting shows non printing characters using ^ or M- notation) ( -v , --show-nonprinting使用 ^ 或 M- 符号显示非打印字符)

This carriage return breaks the formatting and the lines are not processed correctly.此回车符会破坏格式,并且未正确处理行。 Remove the -t flag from the command to get the correct result.从命令中删除-t标志以获得正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM