我是一位新手,想学习bash编程。我不确定什么是“输出到标准输出(stdout)”。这是否意味着将内容打印到命令行?
如果我有一个简单的bash脚本:
#!/bin/bash
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello'
它将一个字符串输出到终端。这是否意味着它正在“输出到标准输出(stdout)”?
谢谢
每个在Linux系统(以及大多数其他系统)上运行的进程都至少有3个打开的文件描述符:
通常,这些文件描述符中的每一个都将指向启动进程的终端。例如:
cat file.txt # all file descriptors are pointing to the terminal where you type the command
然而,Bash允许使用输入/输出重定向来修改这种行为:
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 -q http://192.168.0.1/test -O - | grep -m 1 'Hello' > output.txt
是的,stdout就是终端(除非使用>
运算符将其重定向到文件中或使用|
将其输入到另一个进程的stdin中)。
在您的特定示例中,您实际上是通过| grep ...
通过grep然后重定向到终端。
除非重定向,否则标准输出是启动程序的文本终端。
这里有一篇维基百科文章:http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29