在bash中使用先前命令的输出 [英] Using output of previous commands in bash

查看:69
本文介绍了在bash中使用先前命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mathematica中,可以通过使用%来重用上一个命令的输出.

In Mathematica, it is possible to reuse the output of the previous command by using %.

bash(或其他外壳)是否可能有类似的东西?

Is something similar possible for bash (or some other shell)?

例如,我运行一个给出警告的make,但我想查找所有警告. 所以,我输入

For example, I run a make which gives warnings, but I want to find all warnings. So, I type

make | grep "warning"

但是我看不到make的输出.

but I'm not able to see the output of the make then.

我想改成这样:

make
% | grep "warning"

推荐答案

由于输出的数量不确定,因此bash将其存储起来以供重新显示是没有意义的.但是您的问题还有另一种解决方案:

Since the amount of output is indeterminate, it doesn't make sense for bash to store it for you for re-display. But there's an alternate solution to your problem:

tee命令允许您将输出流复制到文件.因此,如果您愿意使用文件进行临时存储,则可以执行以下操作:

The tee command allows you to duplicate an output stream to a file. So if you're willing to use a file for temporary storage, you can do something like this:

make | tee output.txt
grep "warning" output.txt

此解决方案避免两次运行make,这可能会(a)昂贵且(b)不一致:第二个make的工作量可能少于第一个,因为某些目标是第一次创建的.

This solution avoids running make twice, which could be (a) expensive and (b) inconsistent: the second make may be doing less work than the first because some targets were already made the first time around.

注意:我还没有尝试过.您可能需要摆弄错误和输出流之类的东西.

Note: I haven't tried this. You may need to fiddle with joining the error and output streams, or such.

这篇关于在bash中使用先前命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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