文件标识符(FD)

1. Linux使用文件标识符(FD)来标识一个进程正在访问的特定文件

2. 当打开一个文件或创建一个文件时,Linux将返回一个文件标识符供其他操作引用

3. 文件标识符是一个小的非负整数,他是对应进程的

. 当Linux系统启动一个进程时,将自动为该进程打开三个文件:标准输入(stdin)、标准输出(stdout)、标准错误输出(stderr)

5. 该进程如果要打开其他的输入或输出文件,则从整数3开始标识

我们知道,/proc/N/fd目录包含了进程pid为N的、相关的所有的文件描述符
我们可以看看这个目录下有什么
# ll /proc/1/fd/
总用量 0
lrwx------ 1 root root 64 7月 25 11:22 0 -> /dev/null
lrwx------ 1 root root 64 7月 25 11:22 1 -> /dev/null
lrwx------ 1 root root 64 7月 25 11:22 2 -> /dev/null
lr-x------ 1 root root 64 7月 25 11:22 3 -> pipe:[7399]
l-wx------ 1 root root 64 7月 25 11:22 4 -> pipe:[7399]
lr-x------ 1 root root 64 7月 25 11:22 5 -> inotify
lr-x------ 1 root root 64 7月 25 11:22 6 -> inotify
lrwx------ 1 root root 64 7月 25 11:22 7 -> socket:[7400]
lrwx------ 1 root root 64 7月 25 11:22 9 -> socket:[11307]

stdin、stdout、stderr

1. 标准输入(stdin)、标准输出(stdout)、标准错误输出(stderr)三者的关系是?

shell命令从"标准输入"读取输入数据,将输出送到"标准输出",如果命令在执行过程中发生错误则将错误信息输出到”标准错误输出“

2. stdin、stdout、stderr是怎么来的?

# ll /dev/stdin
lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stdin -> /proc/self/fd/0
# ll /dev/stdout
lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stdout -> /proc/self/fd/1
# ll /dev/stderr
lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stderr -> /proc/self/fd/2

输入输出重定向的符号及其用法

1. 输出重定向:>   >>  >|

在脚本中用的最多的就是把一条命令的执行结果输出重定向到一个文本文件中去

# cat > newfile   --->#把输入重定向到newfile这个文本文件中去
hello
this is a test --->#按ctrl+d结束
# cat newfile
hello
this is a test 注意理解>和>>的区别
# echo "追加内容" >>newfile
# cat newfile
hello
this is a test
追加内容
# echo "覆盖内容" >newfile
# cat newfile
覆盖内容 >|与shell的noclobber选项有关系,表示强制覆盖文件
# set -o noclobber --->#开启此选项
# echo "试图覆盖" >newfile --->#试图覆盖newfile时报错
bash: newfile: cannot overwrite existing file
# echo "强制覆盖" >|newfile --->#可以使用>|强制覆盖
# cat newfile
强制覆盖

2. 重定向标准输出和标准错误的方法

# ll 1.txt 2.txt
ls: 无法访问2.txt: 没有那个文件或目录 --->#这是一条标准错误输出
-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt --->#这是一条标准输出 把错误输出重定向,不显示在屏幕上
# ll 1.txt 2.txt 2>/dev/null
-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt 把标准输出重定向,不显示在屏幕上
# ll 1.txt 2.txt 1>/dev/null
ls: 无法访问2.txt: 没有那个文件或目录 把标准输出和错误输出分别重定向到不同的文件中
# ll 1.txt 2.txt 1>outfile 2>errfile
# cat outfile
-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt
# cat errfile
ls: 无法访问2.txt: 没有那个文件或目录 脚本中最常用的:就是把所有输出都不显示在屏幕上
# ll 1.txt 2.txt &>/dev/null
# ll 1.txt 2.txt >/dev/null 2>&1

3. 在脚本中,while/for/until/if语句的重定向可以按行读取文本文件中的内容,以此实现某种处理

===while循环===
while read line
do
echo $line
done < newfile --->#将newfile文本中的内容按行读取 ===for循环===
ls /etc >loggg
maxline=`wc -l <loggg`
for filename in `seq $maxline`
do
read filename
echo $filename
done <logg

4. 标准输入到标准输出的传递: |(管道)、exec、xargs

(1) -exec 执行后面的命令(其中, “{}”表示存放find命令查找的结果,  “\;”表示结束标志
(2) xargs 是将前面命令的输出做为参数送给后面的命令使用
      -n 后面跟数字num,限制xargs后面的命令接收参数时按num个接收

# find /boot -name "vmlinuz*" | xargs ls -l
-rwxr-xr-x. 1 root root 4044560 Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64 # find /boot -name "vmlinuz*" -exec ls -l {} \;
-rwxr-xr-x. 1 root root 4044560 Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64

[shell基础]——I/O重定向的更多相关文章

  1. Shell基础、输入输出重定向

    1.Shell的功能: (1)Shell是命令解释器,把我们写的命令转化为内核能够识别的机器语言,然后内核调用硬件来完成相应的操作.操作完成后,内核操作结果返回给内核,Shell再将机器语言翻译为我们 ...

  2. shell基础:输入输出重定向

    输出重定向将命令输出存入到文件,类似日志.便于查看.2和>>间没空格.但这种方法没用 ,命令执行时并不知道对错. /dev/null下的null就是一个垃圾箱,脚本中的一些命令并不需要保存 ...

  3. centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课

    centos shell基础知识 alias  变量单引号 双引号   history 错误重定向 2>&1  jobs  环境变量 .bash_history  source配置文件 ...

  4. Linux笔记(shell基础,历史命令,命令补全/别名,通配符,输出重定向)

    一.shell 基础 shell是个命令解释器,提供用户和机器之间的交互 每个用户都可以拥有自己特定的shell centos7默认Shell为bash(Bourne Agin shell) 除了ba ...

  5. shell基础(转)

    shell基础1:文件安全与权限 http://bbs.chinaunix.net/forum/viewtopic.php?t=434579&highlight=wingger 附:Linux ...

  6. Linux实战教学笔记17:精简shell基础

    第十七节 精简shell基础 标签(空格分隔): Linux实战教学笔记 1,前言 1.1 为什么学习shell编程 Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, ...

  7. shell基础及变量

    一 Shell概述 1.Shell的作用——命令解释器,“翻译官” shell作为一个人机接口,用于解释用户输入的命令,将命令解释为Linux内核可以执行的2进制代码,并将执行的结果返回在标准终端上. ...

  8. Linux基础学习(10)--Shell基础

    第十章——Shell基础 一.Shell概述 1.Shell是什么: (1)Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来 ...

  9. Linux Shell基础(下)

    Linux Shell基础(下) 目录 一.shell特殊符号cut命令 二.cut.sort.wc.uniq命令 三.tee.tr.split命令 四.简易审计系统 五.fork, exec, so ...

随机推荐

  1. display:inline-block的空白bug问题

    产生原因:我们写代码的时候习惯在结束标签的后面添加换行符,这个时候就会产生空白符.但是不同浏览器对空白符的理解是不同的,IE6/7会忽略掉此空白符,正常显示内容:IE8以上的IE浏览器以及FF.chr ...

  2. iOS之UIAlertView的使用

    UIAlertView: 1.普通使用: //普通的alert UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title&quo ...

  3. c++ builder TListView控件按字符串排序(根据网上代码亲测ok)

    //--------------------------------------------------------------------------- /* 首先将一个列表框控件安放在Form上, ...

  4. ArrayList总结

    ArrayList 1.extends AbstractList 实现List<E>->Collection<e>->Iterable,RandomAccess,S ...

  5. CAPI3 HTTP文件服务器搭建(共享目录版)

    IIS中运行的程序和WebDAV访问的虚拟目录是共享目录的情况下,非常容易报错,设置如下: 一.程序访问共享目录: 1.配置本地用户IIS权限 2.添加应用池 3.配置连接标示 4.站点关联此应用池即 ...

  6. 网络流量监控shell脚本

    网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可. #!/bin/bash #Usage1,record in fi ...

  7. hdu 2255 奔小康赚大钱 KM算法

    看到这么奇葩的题目名我笑了,后来这么一个裸的KM调了2小时我哭了…… 这是个裸的KM算法,也没什么多说的,主要是注意多组数据时,每次都要把各种数组清空啊,赋值啊什么的,反正比较麻烦.至于为什么调了2小 ...

  8. 获取本地ip地址

    #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { ...

  9. 安装CMS遇到php5.3的问题

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  10. SQL服务器更改名称后

    SQL服务器更改名称后 编写人:CC阿爸 2014-6-15 在日常SQL 2005数据库的操作中,有时安装完成数据库后,再更名,造成部分SQL服务不能正常使用(在SQL2000 时,想都别想更名了) ...