movie.mpeg.001 movie.mpeg.002 movie.mpeg.003 ... movie.mpeg.099
 
$cat movie.mpeg.0*>movie.mpeg
 
 

Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.

Shell 输入/输出重定向

https://ipcmen.com/shell-input-output-redirection

大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端。一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端。同样,一个命令通常将其输出写入到标准输出,默认情况下,这也是你的终端。

重定向命令列表如下:

命令 说明
command > file 将输出重定向到 file。
command < file 将输入重定向到 file。
command >> file 将输出以追加的方式重定向到 file。
n > file 将文件描述符为 n 的文件重定向到 file。
n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m 将输出文件 m 和 n 合并。
n <& m 将输入文件 m 和 n 合并。
<< tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

 
 
 

  1. [xiaole@localhost ~]$ wc -l << EOF
  2. > 欢迎
  3. > you
  4. > 太阳
  5. > EOF
  6. 3
  7. [xiaole@localhost ~]$

  

输出重定向

重定向一般通过在命令间插入特定的符号来实现。特别的,这些符号的语法如下所示:

  1. command1 > file1

上面这个命令执行command1然后将输出的内容存入file1。

注意任何file1内的已经存在的内容将被新内容替代。如果要将新内容添加在文件末尾,请使用>>操作符。

实例

执行下面的 who 命令,它将命令的完整的输出重定向在用户文件中(users):

  1. $ who > users

执行后,并没有在终端输出信息,这是因为输出已被从默认的标准输出设备(终端)重定向到指定的文件。

你可以使用 cat 命令查看文件内容:

  1. $ cat users
  2. _mbsetupuser console Oct 31 17:35
  3. tianqixin console Oct 31 17:35
  4. tianqixin ttys000 Dec 1 11:33

输出重定向会覆盖文件内容,请看下面的例子:

  1. $ echo "IPCMEN:www.ipcmen.com" > users
  2. $ cat users
  3. IPCMENwww.ipcmen.com
  4. $

如果不希望文件内容被覆盖,可以使用 >> 追加到文件末尾,例如:

  1. $ echo "IPCMEN:www.ipcmen.com" >> users
  2. $ cat users
  3. IPCMENwww.ipcmen.com
  4. IPCMENwww.ipcmen.com
  5. $

输入重定向

和输出重定向一样,Unix 命令也可以从文件获取输入,语法为:

  1. command1 < file1

这样,本来需要从键盘获取输入的命令会转移到文件读取内容。

注意:输出重定向是大于号(>),输入重定向是小于号(<)。

实例

接着以上实例,我们需要统计 users 文件的行数,执行以下命令:

  1. $ wc -l users
  2. 2 users

也可以将输入重定向到 users 文件:

  1. $ wc -l < users
  2. 2

注意:上面两个例子的结果不同:第一个例子,会输出文件名;第二个不会,因为它仅仅知道从标准输入读取内容。

  1. command1 < infile > outfile

同时替换输入和输出,执行command1,从文件infile读取内容,然后将输出写入到outfile中。

重定向深入讲解

一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:

  • 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

默认情况下,command > file 将 stdout 重定向到 file,command < file 将stdin 重定向到 file。

如果希望 stderr 重定向到 file,可以这样写:

  1. $ command 2 > file

如果希望 stderr 追加到 file 文件末尾,可以这样写:

  1. $ command 2 >> file

2 表示标准错误文件(stderr)。

如果希望将 stdout 和 stderr 合并后重定向到 file,可以这样写:

  1. $ command > file 2>&1
  2.  
  3. 或者
  4.  
  5. $ command >> file 2>&1

如果希望对 stdin 和 stdout 都重定向,可以这样写:

  1. $ command < file1 >file2

command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2。


Here Document

Here Document 是 Shell 中的一种特殊的重定向方式,用来将输入重定向到一个交互式 Shell 脚本或程序。

它的基本的形式如下:

  1. command << delimiter
  2. document
  3. delimiter

它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

注意:

  • 结尾的delimiter 一定要顶格写,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 缩进。
  • 开始的delimiter前后的空格会被忽略掉。

实例

在命令行中通过 wc -l 命令计算 Here Document 的行数:

  1. $ wc -l << EOF
  2. 欢迎来到
  3. IPCMEN
  4. www.ipcmen.com
  5. EOF
  6. 3 # 输出结果为 3 行
  7. $

我们也可以将 Here Document 用在脚本中,例如:

  1. #!/bin/bash
  2. # author:IPCMEN
  3. # url:www.ipcmen.com
  4.  
  5. cat << EOF
  6. 欢迎来到
  7. IPCMEN
  8. www.ipcmen.com
  9. EOF

执行以上脚本,输出结果:

  1. 欢迎来到
  2. IPCMEN
  3. www.ipcmen.com

/dev/null 文件

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

  1. $ command > /dev/null

/dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到”禁止输出”的效果。

如果希望屏蔽 stdout 和 stderr,可以这样写:

  1. $ command > /dev/null 2>&1

注意:0 是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

https://www.tldp.org/LDP/abs/html/io-redirection.html

I/O Redirection

Chapter 20. I/O Redirection

Table of Contents
20.1. Using exec
20.2. Redirecting Code Blocks
20.3. Applications

There are always three default files [1] open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). These, and any other open files, can be redirected. Redirection simply means capturing output from a file, command, program, script, or even code block within a script (see Example 3-1 and Example 3-2) and sending it as input to another file, command, program, or script.

Each open file gets assigned a file descriptor. [2] The file descriptors for stdinstdout, and stderr are 0, 1, and 2, respectively. For opening additional files, there remain descriptors 3 to 9. It is sometimes useful to assign one of these additional file descriptors to stdin,stdout, or stderr as a temporary duplicate link. [3] This simplifies restoration to normal after complex redirection and reshuffling (see Example 20-1).

  1. COMMAND_OUTPUT >
  2. # Redirect stdout to a file.
  3. # Creates the file if not present, otherwise overwrites it.
  4.  
  5. ls -lR > dir-tree.list
  6. # Creates a file containing a listing of the directory tree.
  7.  
  8. : > filename
  9. # The > truncates file "filename" to zero length.
  10. # If file not present, creates zero-length file (same effect as 'touch').
  11. # The : serves as a dummy placeholder, producing no output.
  12.  
  13. > filename
  14. # The > truncates file "filename" to zero length.
  15. # If file not present, creates zero-length file (same effect as 'touch').
  16. # (Same result as ": >", above, but this does not work with some shells.)
  17.  
  18. COMMAND_OUTPUT >>
  19. # Redirect stdout to a file.
  20. # Creates the file if not present, otherwise appends to it.
  21.  
  22. # Single-line redirection commands (affect only the line they are on):
  23. # --------------------------------------------------------------------
  24.  
  25. 1>filename
  26. # Redirect stdout to file "filename."
  27. 1>>filename
  28. # Redirect and append stdout to file "filename."
  29. 2>filename
  30. # Redirect stderr to file "filename."
  31. 2>>filename
  32. # Redirect and append stderr to file "filename."
  33. &>filename
  34. # Redirect both stdout and stderr to file "filename."
  35. # This operator is now functional, as of Bash 4, final release.
  36.  
  37. M>N
  38. # "M" is a file descriptor, which defaults to 1, if not explicitly set.
  39. # "N" is a filename.
  40. # File descriptor "M" is redirect to file "N."
  41. M>&N
  42. # "M" is a file descriptor, which defaults to 1, if not set.
  43. # "N" is another file descriptor.
  44.  
  45. #==============================================================================
  46.  
  47. # Redirecting stdout, one line at a time.
  48. LOGFILE=script.log
  49.  
  50. echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFILE
  51. echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE
  52. echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE
  53. echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"."
  54. # These redirection commands automatically "reset" after each line.
  55.  
  56. # Redirecting stderr, one line at a time.
  57. ERRORFILE=script.errors
  58.  
  59. bad_command1 2>$ERRORFILE # Error message sent to $ERRORFILE.
  60. bad_command2 2>>$ERRORFILE # Error message appended to $ERRORFILE.
  61. bad_command3 # Error message echoed to stderr,
  62. #+ and does not appear in $ERRORFILE.
  63. # These redirection commands also automatically "reset" after each line.
  64. #=======================================================================
  1. 2>&1
  2. # Redirects stderr to stdout.
  3. # Error messages get sent to same place as standard output.
  4. >>filename 2>&1
  5. bad_command >>filename 2>&1
  6. # Appends both stdout and stderr to the file "filename" ...
  7. 2>&1 | [command(s)]
  8. bad_command 2>&1 | awk '{print $5}' # found
  9. # Sends stderr through a pipe.
  10. # |& was added to Bash 4 as an abbreviation for 2>&1 |.
  11.  
  12. i>&j
  13. # Redirects file descriptor i to j.
  14. # All output of file pointed to by i gets sent to file pointed to by j.
  15.  
  16. >&j
  17. # Redirects, by default, file descriptor 1 (stdout) to j.
  18. # All stdout gets sent to file pointed to by j.
  1. 0< FILENAME
  2. < FILENAME
  3. # Accept input from a file.
  4. # Companion command to ">", and often used in combination with it.
  5. #
  6. # grep search-word <filename
  7.  
  8. [j]<>filename
  9. # Open file "filename" for reading and writing,
  10. #+ and assign file descriptor "j" to it.
  11. # If "filename" does not exist, create it.
  12. # If file descriptor "j" is not specified, default to fd 0, stdin.
  13. #
  14. # An application of this is writing at a specified place in a file.
  15. echo 1234567890 > File # Write string to "File".
  16. exec 3<> File # Open "File" and assign fd 3 to it.
  17. read -n 4 <&3 # Read only 4 characters.
  18. echo -n . >&3 # Write a decimal point there.
  19. exec 3>&- # Close fd 3.
  20. cat File # ==> 1234.67890
  21. # Random access, by golly.
  22.  
  23. |
  24. # Pipe.
  25. # General purpose process and command chaining tool.
  26. # Similar to ">", but more general in effect.
  27. # Useful for chaining commands, scripts, files, and programs together.
  28. cat *.txt | sort | uniq > result-file
  29. # Sorts the output of all the .txt files and deletes duplicate lines,
  30. # finally saves results to "result-file".

Multiple instances of input and output redirection and/or pipes can be combined in a single command line.

  1. command < input-file > output-file
  2. # Or the equivalent:
  3. < input-file command > output-file # Although this is non-standard.
  4.  
  5. command1 | command2 | command3 > output-file

See Example 16-31 and Example A-14.

Multiple output streams may be redirected to one file.

  1. ls -yz >> command.log 2>&1
  2. # Capture result of illegal options "yz" in file "command.log."
  3. # Because stderr is redirected to the file,
  4. #+ any error messages will also be there.
  5.  
  6. # Note, however, that the following does *not* give the same result.
  7. ls -yz 2>&1 >> command.log
  8. # Outputs an error message, but does not write to file.
  9. # More precisely, the command output (in this case, null)
  10. #+ writes to the file, but the error message goes only to stdout.
  11.  
  12. # If redirecting both stdout and stderr,
  13. #+ the order of the commands makes a difference.

Closing File Descriptors

n<&-

Close input file descriptor n.

0<&-, <&-

Close stdin.

n>&-

Close output file descriptor n.

1>&-, >&-

Close stdout.

Child processes inherit open file descriptors. This is why pipes work. To prevent an fd from being inherited, close it.

  1. # Redirecting only stderr to a pipe.
  2.  
  3. exec 3>&1 # Save current "value" of stdout.
  4. ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls').
  5. # ^^^^ ^^^^
  6. exec 3>&- # Now close it for the remainder of the script.
  7.  
  8. # Thanks, S.C.

For a more detailed introduction to I/O redirection see Appendix F.

Notes

[1]

By convention in UNIX and Linux, data streams and peripherals (device files) are treated as files, in a fashion analogous to ordinary files.

[2]

file descriptor is simply a number that the operating system assigns to an open file to keep track of it. Consider it a simplified type of file pointer. It is analogous to a file handle in C.

[3]

Using file descriptor 5 might cause problems. When Bash creates a child process, as with exec, the child inherits fd 5 (see Chet Ramey's archived e-mail, SUBJECT: RE: File descriptor 5 is held open). Best leave this particular fd alone.


file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes 禁止输出 屏蔽 stdout 和 stderr 输入输出重定向 重定向文件描述符的更多相关文章

  1. file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes

    $>emtry_or_create_a_file.f $ll>>append_a_file.f standard output input error $ls -l /usr/bin ...

  2. [svc]linux中的文件描述符(file descriptor)和文件

    linux中的文件描述符(file descriptor)和文件 linux为了实现一切皆文件的设计哲学,不仅将数据抽象成了文件,也将一切操作和资源抽象成了文件,比如说硬件设备,socket,磁盘,进 ...

  3. (文件描述符0、1、2),(stdin、stdout、stderr),(终端设备)这三者之间的关系???

    前言 在Linux系统中,一切设备都看作文件.而每打开一个文件,就有一个代表该打开文件的文件描述符.程序启动时默认打开三个I/O设备文件:标准输入文件stdin,标准输出文件stdout,标准错误输出 ...

  4. Linux exec与文件描述符

    看到好几篇文章讲述exec都是一知半解,所以我尽量说的清楚明白一些.本文首先讲述Linux文件描述符,然后是exec,最后举例说明exec I/O重定向及其用法. 概念:exec命令用于调用并执行指令 ...

  5. Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1

    linux空设备文件和重定向 输出/输入重导向 >      >>   <   <<   :>   &>   2&>   2< ...

  6. Linux Shell 文件描述符 及 stdin stdout stderr 重定向

    Abstract: 1) Linux Shell 命令的标准输入.标准输出.标准错误,及其重定位: 2)Linux Shell 操作自定义文件描述符: 文件描述符是与文件相关联的一些整数,他们保持与已 ...

  7. 终端设备 tty,pty,pts 概念与文件描述符的联系

    第1节 理解终端设备tty.pty.pts概念 简要描述: 终端设备默认具有输入.输出功能. 现代我们最常用的接入服务器端的方式(如:ssh通过tcp/ip的方式连接服务器端,作为服务器的终端设备)为 ...

  8. 玩转Linux文件描述符和重定向

    本文介绍linux中文件描述符与重定向的相关知识,文件描述符是与文件输入.输出相关联的整数,它们用来跟踪已打开的文件.有需要的朋友参考下. 原文出处:http://www.jbxue.com/arti ...

  9. Linux下的文件操作——基于文件描述符的文件操作(2)

    文件描述符的复制 MMAP文件映射 ftruncate修改文件大小 文件描述符的复制 ​ 系统调用函数dup和dup2可以实现文件描述符的复制,经常用来重定向进程的stdin(0), stdout(1 ...

随机推荐

  1. Spring Boot 使用常见问题

    Json格式化时间,时区设置 spring.jackson.time-zone=GMT+8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss json数据无 ...

  2. java中将文件夹里面的文件复制到指定的文件夹(java IO)

    //现在制定路径下创建名称为左侧的文件夹 public class Copy { public static void main(String[] args) { //原始文件地址 File srcF ...

  3. DVWA Brute Force:暴力破解篇

    DVWA Brute Force:暴力破解篇 前言 暴力破解是破解用户名密码的常用手段,主要是利用信息搜集得到有用信息来构造有针对性的弱口令字典,对网站进行爆破,以获取到用户的账号信息,有可能利用其权 ...

  4. esx.problem.hyperthreading.unmitigated

    是因为VMware新发布的一个漏洞补丁导致的具体解释可参阅VMware官方kb,有详细解释和版本说明. 可选择屏蔽该问题告警 选中主机>配置>高级系统设置>编辑>修改" ...

  5. springboot容器启动顺序之@Configuration ContextRefreshedEvent事件初始化 ApplicationRunner

    笔者最近遇到一个问题 我们根据自己业务需要  需要首次启动springboot项目时 把数据库数据同步至本地缓存(比如ehcache)但有一个要求 在缓存未载入成功  不允许有流量打入 一开始我们使用 ...

  6. jQuery EasyUI学习一

    1.   jQuery EasyUI介绍 1.  创建组件的方式和原理(掌握) 2.  组件三要素(掌握) 3.  Panel.LinkButton.上下文菜单;(掌握) 简介 2.1.  jQuer ...

  7. 新建表后,在sqlserver manager中使用显示引用对象无效

    编辑>intelliSense(I)>刷新本地缓存.

  8. 大数据量查询容易OOM?试试MySQL流式查询

    一.前言 程序访问 MySQL 数据库时,当查询出来的数据量特别大时,数据库驱动把加载到的数据全部加载到内存里,就有可能会导致内存溢出(OOM). 其实在 MySQL 数据库中提供了流式查询,允许把符 ...

  9. VUE项目性能优化实践——通过懒加载提升页面响应速度

    本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 最近我司因业务需求,需要在一个内部数据分析平台集成在线Excel功能,既然我 ...

  10. ceph对接k8s storage class

    简介 对接ceph的rbd和cephfs到k8s中提供持久化存储 环境 主机名 IP role 操作系统 ceph-01 172.16.31.11 mon osd CentOS7.8 ceph-02 ...