'输出到stdout'是什么意思

时间:2013-04-10 23:29:47

标签: linux bash

bash编程新手。我不确定输出到stdout'是什么意思。这是否意味着打印到命令行?

如果我有一个简单的bash脚本:

#!/bin/bash
wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello'

它向终端输出一个字符串。这是否意味着它输出到stdout' ?

由于

3 个答案:

答案 0 :(得分:3)

是的,stdout是终端(除非使用>运算符重定向到文件或使用|重定向到另一个进程的标准输入中

在您的具体示例中,您实际上是使用| grep ...通过grep重定向到终端。

答案 1 :(得分:2)

Linux系统(以及大多数其他系统)上的每个进程至少有3个打开的文件描述符:

  • stdin(0)
  • stdout(1)
  • stderr(2)

每个文件描述符都将指向启动进程的终端。像这样:

cat file.txt # all file descriptors are pointing to the terminal where you type the      command

但是,bash允许使用input / output redirection修改此行为:

cat < file.txt # will use file.txt as stdin

cat file.txt > output.txt # redirects stdout to a file (will not appear on terminal anymore)

cat file.txt 2> /dev/null # redirects stderr to /dev/null (will not appear on terminal anymore

当您使用管道符号时会发生同样的情况:

wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello'

实际发生的是wget进程的stdout(|之前的进程)被重定向到grep进程的stdin。因此,当grep的输出是当前终端时,wget的stdout不再是终端。例如,如果要将grep的输出重定向到文件,请使用:

wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello' > output.txt

答案 2 :(得分:1)

除非重定向,否则标准输出是启动程序的文本终端。

这是一篇维基百科文章:http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29

相关问题