文件标识符(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. css兼容

    1.不同浏览器默认边距不同,必须对body自定义:margin:0;padding:0; 2.margin.padding属性值为%时,不是所有浏览器都支持: 3.transparent属性,IE7之 ...

  2. MITM to crack Https connections

    Everybody knows that https is http over SSL, and https is a secure way for protecting confidential d ...

  3. Windows Server 2008 R2 配置Exchange 2010邮件服务器

    windows server 服务器系统搭建邮件服务器一般两种情况: 1:Winmail server 软件 2:Exchange 参考教程:http://www.cnblogs.com/zhongw ...

  4. Linux下解压命令

    .tar.xz 先用命令[xz -d ***.tar.xz]解压,然后用[tar xvf ***.tar]解包 也可以直接用命令[tar xvJf ***.tar.xz]解压

  5. Linux下运行top命令显示的PR\NI\RES\SHR\S\%MEM TIME+都代表什么

    PID 进程号 USER 用户名 PR 优先级 NI nice值.负值表示高优先级,正值表示低优先级 RES 进程使用的.未被换出的物理内存大小,单位Kb S 进程状态: D 不可中断的睡眠状态 R ...

  6. Android IOS WebRTC 音视频开发总结(五二)-- 亲,咱一起采访webrtc大会的各路专家

    本文最早发自我的微信公众号,咱一起采访webrtc大会的各路专家,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处. 亲,作为webrtc大会的推动者之一,大会期间不管是公共场合还是私 ...

  7. dwz简单配置与操作

    1.首先将dwz的文件放到你的项目中(http://yunpan.cn/QbTH4kN6UXX9B) 2.在页面中将前台数据复制到页面中,将js,css等路径配置好 3.这个地方一定要配置好,xml文 ...

  8. Excel导入数据(97--2003版本)的ExcelHelper

    首先确定excel的版本为97~2003版本 其次创建一个帮助类——ExcelHelper //单个sheet public static DataTable AnalysisExcel(string ...

  9. 文本处理命令--wc、sed

    一.wc wc命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 测试文件内容: (my_python_env)[root@hadoop26 ~]# cat test hnlinu ...

  10. 登录成功返回登录前页面js代码

    /*------ setCookie(name,value) -----------*/ function setCookie(name,value) { var Days = 30; //此 coo ...