bash – 如何捕获任何shell输出?

前端之家收集整理的这篇文章主要介绍了bash – 如何捕获任何shell输出?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们在浏览器中设计一个 shell,它将流输出 shell输出的所有内容. stdout,stderr,什么的.

然而,正如这个小例子向你展示的那样,

git clone https://github.com/joyent/node.git >./git.out 
cat ./git.out

大部分输出都没有给./git.out.这里是小视频,如果你想看看http://screencast.com/t/7zTdPfq7Pr7正在发生什么

我们需要像’top’,’git’这样的命令才能工作,任何想法如何获得它们?

(输出文件仅用于说明目的,我们会将每行输出流式传输到另一个系统,我们知道最重要的是它没有意义但我希望我的观点足够清楚,以便能够提供洞察力的人.谢谢. )

大多数程序有两个输出:stdout和stderr. stdout是“主要”信息的去向. stderr用于,错误.它也用于各种非重要输出,有时也用于用户提示等.

要捕获所有这些,您需要将stderr重定向到stdout.

somecmd arg1 arg2 arg3 >somefile 2>&1

或者将stderr捕获到单独的文件

somecmd arg1 arg2 arg3 >stdout.txt 2>stderr.txt

请注意,程序可以使用除stdout / stderr之外的其他输出.它可以确定你的控制台,并在那里基本上“强制”输出.然而,这种情况非常罕见,上述情况将在99%的情况下发挥作用.

如果将stdout和stderr重定向到同一个文件,请记住一件事是stdout和stderr缓冲区不同,因此它在日志文件中看起来与在屏幕上看起来不一样(行将不按顺序排列) ).此外,对于您的特定实例,您的输出将看起来像垃圾. git做了很多终端操作(对于进度指示器),在日志文件中看起来非常糟糕.

编辑

来自man git-clone:

–progress Progress status is reported on the standard error stream by default when it is attached to a terminal,unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal

原文链接:https://www.f2er.com/bash/385555.html

猜你在找的Bash相关文章