美文网首页Linux
Linux - shell 重定向

Linux - shell 重定向

作者: 谢小帅 | 来源:发表于2017-05-02 19:47 被阅读6次

linux启动后,会默认打开3个文件描述符。

  • 标准输入:standard input 0
  • 正确输出:standard output 1
  • 错误输出:error output 2

所有运行的shell命令,都会有这3个默认文件描述符。

linux shell下常用输入输出操作符

文件描述符 对应代码 指令 软链接对应位置
标准输入 (stdin) 0 < 或 << /dev/stdin -> /proc/self/fd/0
标准输出 (stdout) 1 > 或 >> /dev/stdout -> /proc/self/fd/1
标准错误输出(stderr) 2 2> 或 2>> /dev/stderr -> /proc/self/fd/2

输入输出的方向不要和C++的cin,cout混淆

cin >> a; // 是反过来的,根据重定向的字面意思理解
cout << "hello world";
shuai@ubuntu:/proc/self/fd$ ls
0  1  2  255
shuai@ubuntu:~$ ls
a.txt  Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos
shuai@ubuntu:~$ cat a.txt 
where
who
when
what
why

// b.txt文件不存在
shuai@ubuntu:~$ cat a.txt b.txt 1>suc.txt 2>err.txt
shuai@ubuntu:~$ cat suc.txt 
where
who
when
what
why
shuai@ubuntu:~$ cat err.txt 
cat: b.txt: No such file or directory

Linux shell 重定向

相关文章

网友评论

    本文标题:Linux - shell 重定向

    本文链接:https://www.haomeiwen.com/subject/ehrhtxtx.html