- 论坛徽章:
- 0
|
刚在学习unix高级编程这本书
有这个程序:
- /* tty.c */
- #include <stdio.h>;
- #include <unistd.h>;
- void tty_info(int fd)
- {
- int b = isatty(fd);
- printf("fd=%d %s a tty\n", fd, b?"is":"isn`t");
- if (b)
- {
- printf("tty name is '%s'\n", ttyname(fd));
- }
- }
- int main(int argc, char **argv)
- {
- tty_info(0); /* Query standard input */
- tty_info(1); /* Query standard output */
- tty_info(2); /* Query standard error */
- return 0;
- }
复制代码
编译后执行 ./tty
fd=0 is a tty //标准输入
tty name is '/dev/ttyp2'
fd=1 is a tty //标准输出
tty name is '/dev/ttyp2'
fd=2 is a tty //错误
tty name is '/dev/ttyp2'
这是判断 标准输入描述符,标准输出描述符以及错误描述符是否是tty设备的吧?
我在运行
./tty 2>;/dev/null 时出现
- fd=0 is a tty
- tty name is '/dev/pts/2'
- fd=1 is a tty
- tty name is '/dev/pts/2'
- fd=2 isn`t a tty
复制代码
这是是 标准输入描述符,标准输出描述符是tty设备,而错误描述符不是,上面的这个命令是把 2 写入到 /dev/null文件中吧?但为什么会出现这个结果
还有按照书上的
./tty 2>;/dev/null </dev/null <=这个的意思,我没看懂,抱歉,请朋友帮忙解释一下, 我对LINUX命令,不太熟悉。
出现
- fd=0 isn`t a tty
- fd=1 is a tty
- tty name is '/dev/pts/2'
- fd=2 isn`t a tty
复制代码
这时只有 标准输出描述符是tty设备 了, 我都糊涂了~ 请理解的朋友能给我讲讲相关的知识么?感谢 |
|