Linux 标准文件描述符
出于特殊目的,bash shell保留了最早的3个文件描述符0、1、2,如下所示:

1、STDIN标准输入
Shell从STDIN文件描述对应的键盘获得输入,在用户输入时处理每个字符。
范例1:cat命令处理STDIN输入的数据,输入一个就显示一个,按ctrl+c即可结束。
[root@cloucentos6 home]# cat
my
my
name
name
is
is
TOM
TOM
范例2:使用输入重定向符号 < 时,Linux会重写向指定的文件来替换文字的输入文件描述符。
[root@cloucentos6 home]# cat file
my name is Tom.
[root@cloucentos6 home]# cat < file
my name is Tom.
2、STDOUT标准输出
shell所有的输出(包括shell中运行的程序和脚本)会被定向到标准输出中,就是在终端上显示出来。
范例1:使用输出重定向 > 来改变输出。
[root@cloucentos6 home]# ls -l > file
[root@cloucentos6 home]# cat file
总用量 20
-rw-r--r--. 1 root root 0 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
范例2:使用输出重写向 >> 可以追加某个文件。
[root@cloucentos6 home]# ls -l >> file
[root@cloucentos6 home]# cat file
总用量 20
-rw-r--r--. 1 root root 0 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
总用量 24
-rw-r--r--. 1 root root 171 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
3、STDERR标准错误
shell运行程序和脚本出错时生成的错误消息都会发送到这个位置。
范例1:只重定向错误,STERR文件描述符被设成2,ls 查看一个/home目录不存在的文件将错误输出到file文件,屏幕上不会显示错误。
[root@cloucentos6 home]# ls -l test4 2>file
[root@cloucentos6 home]# cat file
ls: 无法访问test4: 没有那个文件或目录
范例2:重写向错误和数据,如果想重写向错误和正常输出,必须用两个重写向符号。STDOUT文件描述符被设为1,STERR文件描述符被设成2。
[root@cloucentos6 home]# ls
lost+found test.sh
[root@cloucentos6 home]# ls -l test.sh test4 2>file1 1>file2
[root@cloucentos6 home]# ls
file1 file2 lost+found test.sh
[root@cloucentos6 home]# cat file1
ls: 无法访问test4: 没有那个文件或目录
[root@cloucentos6 home]# cat file2
-rwx------. 1 root root 81 6月 23 23:32 test.sh
范例3:假如把STDERR和STDOUT输出重定向到同一个文件file中,bash shell提供了一个特殊的重定向符号 &>
[root@cloucentos6 home]# ls
lost+found test.sh
[root@cloucentos6 home]# ls -l test.sh test2 &>file
[root@cloucentos6 home]# cat file
ls: 无法访问test2: 没有那个文件或目录
-rwx------. 1 root root 81 6月 23 23:32 test.sh
4、在脚本中重写向输出
范例1:在脚本中临时指定某行重定向输出标准错误和标准
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
bit=`arch`
echo "hello ! world~!"
echo "系统位数 $bit" >&2
ipocnfig >&2
[root@cloucentos6 home]# ./test.sh 2>out
hello ! world~!
[root@cloucentos6 home]# cat out
系统位数 x86_64
./test.sh: line 5: ipocnfig: command not found
范例2:永久重定向输出脚本中所有标准错误
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
exec 2>/home/out
bit=`arch`
echo "hello ! world~!"
echo "系统位数 $bit"
ipocnfig
[root@cloucentos6 home]# ./test.sh
hello ! world~!
系统位数 x86_64
[root@cloucentos6 home]# cat out
./test.sh: line 7: ipocnfig: command not found
范例3:永久重定向输出脚本中所有标准错误和标准输出
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
exec &>/home/out
bit=`arch`
echo "hello ! world~!"
echo "系统位数 $bit"
ipocnfig
[root@cloucentos6 home]# ./test.sh
[root@cloucentos6 home]# cat out
hello ! world~!
系统位数 x86_64
./test.sh: line 7: ipocnfig: command not found
5、阻止命令输出
如果你想shell脚本后台运行输出所有的信息都要丢掉,不想显示出来,可以把Shell重定向到一个称作null文件的特殊文件,只要输出到null文件的任何数据都不会保存,这样所有的输出都会被丢掉。
范例1:Linux系统上的null文件的标准位置是/dev/null,你重定向到该位置的任何数据都会被丢掉,不会显示:
[root@cloucentos6 home]# ls -l > /dev/null
[root@cloucentos6 home]# cat /dev/null
范例2:阻止任何错误消息而不保存它们的一个通用方法
[root@cloucentos6 home]# ls -al test4.txt test.sh 2>/dev/null
-rwx------. 1 root root 98 6月 26 19:09 test.sh
范例3:由于/dev/null文件不含有任何内容,重定向输入将/dev/null作为输入文件,可以使用它快速清空现有文件中的所有数据,不用先删除文件再创建。
[root@cloucentos6 home]# cat out
hello ! world~!
[root@cloucentos6 home]# cat /dev/null > /home/out
[root@cloucentos6 home]# cat out
6、记录消息
tee命令相当于管道的一个T型接头,可以从标准输入过来的数据同时发给两个上的地。一个目的地是标准输出,另一个目的地是tee命令行所指定的文件名。
范例1:tee将从STDIN过来的数据重定向。
[root@cloucentos6 home]# date | tee out
2017年 06月 27日 星期二 00:18:28 CST
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
范例2:默认情况下,tee命令会在每次使用时覆盖输出文件,如果想将数据追加到文件中,必须使用 – a 选项。
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
root@cloucentos6 home]# who | tee -a out
root pts/0 2017-06-27 00:14 (10.8.9.11)
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
root pts/0 2017-06-27 00:14 (10.8.9.11)
[root@cloucentos6 home]# arch | tee out
x86_64
[root@cloucentos6 home]# cat out
x86_64
范例3:脚本中把数据保存到文件中,也能把数据显示在屏幕上(但tee命令输出的错误信息不会保存在文件中)。
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
bit=`arch`
echo "hello ! world~!" | tee out
echo "系统位数 $bit" | tee -a out
ipocnfig | tee -a out
[root@cloucentos6 home]# ./test.sh
hello ! world~!
系统位数 x86_64
./test.sh: line 5: ipocnfig: command not found
[root@cloucentos6 home]# cat out
hello ! world~!
系统位数 x86_64
Linux 标准文件描述符的更多相关文章
- Linux 文件描述符详解
Overview 了解Linux怎样处理输入和输出是非常重要的.一旦我们了解其原理以后,我们就可以正确熟练地使用脚本把内容输出到正确的位置.同样我们也可以更好地理解输入重定向和输出重定向. Linux ...
- Linux文件描述符与打开文件之间的区别(转载)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为: ...
- linux文件描述符--转载
转自:http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录 ...
- 玩转Linux文件描述符和重定向
本文介绍linux中文件描述符与重定向的相关知识,文件描述符是与文件输入.输出相关联的整数,它们用来跟踪已打开的文件.有需要的朋友参考下. 原文出处:http://www.jbxue.com/arti ...
- [转帖]linux文件描述符文件/etc/security/limits.conf
linux文件描述符文件/etc/security/limits.conf https://blog.csdn.net/fanren224/article/details/79971359 需要多学习 ...
- [fw]LINUX中断描述符初始化
LINUX中断描述符初始化 @CopyLeft by ICANTH,I Can do ANy THing that I CAN THink!~ Author: WenHui, WuHan Univer ...
- linux文件描述符、软硬连接、输入输出重定向
引用链接:https://blog.csdn.net/qq769651718/article/details/79459346 文件描述符的作用: 文件描述符是linux操作系统中特有的概念.其相当于 ...
- [性能分析]linux文件描述符(转)
1.什么是文件和文件描述符 Linux中文件可以分为4种:普通文件.目录文件.链接文件和设备文件.1.普通文件是用户日常使用最多的文件,包括文本文件.shell脚本.二进制的可执行和各种类型的数据.l ...
- linux 文件描述符
文件描述符是什么?和文件句柄有啥区别?文件描述符是linux/unix操作系统中特有的概念.相当于windows系统中的文件句柄.一个意思不同叫法.Linux系统中, 每当进程打开一个文件时,系统就为 ...
随机推荐
- Ubuntu首次安装后root权限解锁
在ubuntu系统下,为了安全起见,在安装过程中,系统屏蔽了用户设置root用户.导致很多用户在使用过程中不知道root密码到底是什么. 可以使用如下方法解决: 先解除root锁定,为root用户设置 ...
- java中的动态加载和热替换
https://blog.csdn.net/u010833547/article/details/54312052 ****************************************** ...
- mysql 系统表的作用
mysql 的系统表记录了所有数据库表(包括视图的定义语句)的字段列,顺序,类型等等,知道这些的话可以做些抽取模板淫荡的操作吧 嘿嘿 public void shuaxinglb() { try { ...
- 再论FreeRTOS中的configTOTAL_HEAP_SIZE
关于任务栈和系统栈的基础知识,可以参考之前的随笔.(点击这里) 这里再次说明:#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 17 * 1024 ) ) 这个 ...
- C语言版——点亮LED灯,深入到栈
在上一篇进行了汇编语言的编写之后,我们采用C语言来编写程序,毕竟C语言才是我们使用最多的语言. 仅仅是点亮LED灯显然太过于简单,我们需要分析最后的反汇编,了解函数调用栈,深入C语言骨髓去分析代码,并 ...
- docker探索-CentOS7中配置Docker的yum源并升级安装docker1.13(十)
此处使用的是CentOS7,内核版本为 [root@localhost ~]# uname -r -.el7.x86_64 该版本下,配置了yum的源为阿里的镜像源,具体的配置方法可以参见阿里镜像源配 ...
- ntpd与ntpdate的区别
之前配置ntpd的时候搜到一句话,印象很深刻,也觉得很有标题党的效果,就借鉴为标题了:“我认为有几种人是必须不招聘/裁掉的: 1 用ntpdate代替ntpd的人”但具体原因不太懂,总觉得还是用ntp ...
- 【Android】按钮点击事件的常用写法
学习总结: 最近学习了Android点击事件的常用写法.点击事件会触发监听对象身上的回调,常用写法有以下四种: 方法一:使用匿名内部类. public class MainActivity exten ...
- 【C#公共帮助类】JsonHelper 操作帮助类
四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换js ...
- textmate常用快捷键备忘
视图切换 Ctrl + Cmd + F # 折叠第三层 代码编辑 Cmd + Shift + V # 按照历史拷贝顺序来粘贴 Ctrl + Cmd + Option + V # 显示剪贴板 Cmd + ...