如何将bash命令输出到stdout并同时通过管道传输到另一个命令? [英] How to output bash command to stdout and pipe to another command at the same time?

查看:296
本文介绍了如何将bash命令输出到stdout并同时通过管道传输到另一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上工作,为了显示详细的GPU信息,我使用以下命令:

I'm working on a server and to show detailed GPU information I use these commands:

nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

但是,如您所见,nvidia-smi被调用了两次.如何使nvidia-smi的输出转至输出并同时通过管道传输到另一个命令?

However as you can see, nvidia-smi is called twice. How can I make the output of nvidia-smi go to output and pipe to another command at the same time?

推荐答案

使用tee:

ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

由于stdout已通过管道传输,因此您无法对其进行复制,因此我选择了stderr来显示输出.

Since stdout is piped, you can't make a copy to it, so I picked stderr to show output.

如果/dev/stderr不可用,请使用/proc/self/fd/2.

这篇关于如何将bash命令输出到stdout并同时通过管道传输到另一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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