In Mathematica, it is possible to reuse the output of the previous command by using %.
在Mathematica中,可以使用%重用上一个命令的输出。
Is something similar possible for bash (or some other shell)?
bash(或其他一些shell)有类似的东西吗?
For example, I run a make which gives warnings, but I want to find all warnings. So, I type
例如,我运行一个make,它会发出警告,但我想找到所有警告。所以,我输入
make | grep "warning"
but I'm not able to see the output of the make then.
但是我无法看到make的输出。
I would like to type something like this instead:
我想输入这样的东西:
make
% | grep "warning"
4 个解决方案
#1
9
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:
由于输出量是不确定的,因此bash存储它以供您重新显示是没有意义的。但是有一个替代解决方案可以解决您的问题:
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:
tee命令允许您将输出流复制到文件。因此,如果您愿意使用文件进行临时存储,您可以执行以下操作:
make | tee output.txt
grep "warning" output.txt
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.
这个解决方案避免了两次运行make,这可能是(a)昂贵和(b)不一致:第二个make可能比第一个做的工作少,因为有些目标已经是第一次出现了。
Note: I haven't tried this. You may need to fiddle with joining the error and output streams, or such.
注意:我没试过这个。您可能需要摆弄加入错误和输出流等。
#2
2
I'm not sure if the make
command sends warnings to stderr but I suspect it does. try this
我不确定make命令是否会向stderr发送警告,但我怀疑它确实如此。尝试这个
make 2&>1 |grep 'warning'
it will redirect stderr to stdout.
它会将stderr重定向到stdout。
Should also note that you can't grep anything that's not going to stdout.
还应该注意,你不能grep任何不会stdout的东西。
#3
2
If you use tee to duplicate the output stream to /dev/stderr, there's no need for a temp file; plus, after that, you can filter the stdout stream with sed to create a make_warning.log file - all in one line of Unix shell pipes.
如果使用tee将输出流复制到/ dev / stderr,则不需要临时文件;此外,您可以使用sed过滤stdout流以创建make_warning.log文件 - 所有这些都在Unix shell管道中。
make 2>&1 | tee /dev/stderr | \
sed -E -n 's/(.*[Ww][Aa][Rr][Nn][Ii][Nn][Gg].*)/\1/p' > make_warning.log
#4
2
Swap the stdout
and stderr
streams to log the entire stderr
stream:
交换stdout和stderr流以记录整个stderr流:
make 3>&2 2>&1 1>&3 3>&- | tee /dev/stderr > stderr.log
#1
9
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:
由于输出量是不确定的,因此bash存储它以供您重新显示是没有意义的。但是有一个替代解决方案可以解决您的问题:
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:
tee命令允许您将输出流复制到文件。因此,如果您愿意使用文件进行临时存储,您可以执行以下操作:
make | tee output.txt
grep "warning" output.txt
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.
这个解决方案避免了两次运行make,这可能是(a)昂贵和(b)不一致:第二个make可能比第一个做的工作少,因为有些目标已经是第一次出现了。
Note: I haven't tried this. You may need to fiddle with joining the error and output streams, or such.
注意:我没试过这个。您可能需要摆弄加入错误和输出流等。
#2
2
I'm not sure if the make
command sends warnings to stderr but I suspect it does. try this
我不确定make命令是否会向stderr发送警告,但我怀疑它确实如此。尝试这个
make 2&>1 |grep 'warning'
it will redirect stderr to stdout.
它会将stderr重定向到stdout。
Should also note that you can't grep anything that's not going to stdout.
还应该注意,你不能grep任何不会stdout的东西。
#3
2
If you use tee to duplicate the output stream to /dev/stderr, there's no need for a temp file; plus, after that, you can filter the stdout stream with sed to create a make_warning.log file - all in one line of Unix shell pipes.
如果使用tee将输出流复制到/ dev / stderr,则不需要临时文件;此外,您可以使用sed过滤stdout流以创建make_warning.log文件 - 所有这些都在Unix shell管道中。
make 2>&1 | tee /dev/stderr | \
sed -E -n 's/(.*[Ww][Aa][Rr][Nn][Ii][Nn][Gg].*)/\1/p' > make_warning.log
#4
2
Swap the stdout
and stderr
streams to log the entire stderr
stream:
交换stdout和stderr流以记录整个stderr流:
make 3>&2 2>&1 1>&3 3>&- | tee /dev/stderr > stderr.log