我想管道标准输出的程序,同时保持它在屏幕上。

通过一个简单的例子(这里使用echo只是为了说明):

$ echo 'ee' | foo ee <-我想看到的输出

我知道tee可以复制stdout到文件,但这不是我想要的。 $ echo 'ee' | tee output.txt | foo

我试着 $ echo 'ee' | tee /dev/stdout | foo但它不起作用,因为tee输出到/dev/stdout是通过管道输出到foo的

我如何从一个shell脚本中检测,如果它的标准输出被发送到终端或如果它被管道到另一个进程?

举个例子:我想添加转义码来着色输出,但只在交互运行时,而不是在管道运行时,类似于ls——color。

如何使用subprocess.call()获得进程运行的输出?

传递一个StringIO。StringIO对象的stdout给出以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>> 

我想使用subprocess.check_output() ps -A | grep 'process_name'。 我尝试了各种解决方案,但到目前为止都没用。有人能指导我怎么做吗?

我想在Bash中执行一个长时间运行的命令,并捕获它的退出状态,并输出它的输出。

所以我这样做:

command | tee out.txt
ST=$?

问题是变量ST捕获tee的退出状态,而不是命令的退出状态。我怎么解决这个问题?

注意,该命令长时间运行,将输出重定向到文件以供以后查看,这对我来说不是一个好的解决方案。

我正在尝试将Windows命令的所有输出(stdout+stderr)重定向到单个文件:

C:\>dir 1> a.txt 2> a.txt
The process cannot access the file because it is being used by another process.

这是可能的,还是我应该重定向到两个单独的文件?

我有一个程序,将信息写入标准输出和标准错误,我需要用grep处理标准错误,把标准错误放在一边。

使用临时文件,可以分两步完成:

command > /dev/null 2> temp.file
grep 'something' temp.file

但是,在没有临时文件的情况下,如何使用一个命令和管道实现这一点呢?