⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ioredirintro.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>A Detailed Introduction to I/O and I/O Redirection</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Exit Codes With Special Meanings"HREF="exitcodes.html"><LINKREL="NEXT"TITLE="Command-Line Options"HREF="command-line-options.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="exitcodes.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="command-line-options.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="IOREDIRINTRO"></A>Appendix E. A Detailed Introduction to I/O and I/O Redirection</H1><P><SPANCLASS="emphasis"><ICLASS="EMPHASIS">written by St閜hane Chazelas, and revised        by the document author</I></SPAN></P><P><ANAME="STDINOUTDEF"></A></P><P>A command expects the first three <AHREF="io-redirection.html#FDREF">file        descriptors</A> to be available. The first, <ICLASS="FIRSTTERM">fd        0</I> (standard input, <TTCLASS="FILENAME">stdin</TT>),        is for reading. The other two (<ICLASS="FIRSTTERM">fd 1</I>,        <TTCLASS="FILENAME">stdout</TT> and <ICLASS="FIRSTTERM">fd 2</I>,        <TTCLASS="FILENAME">stderr</TT>) are for writing.</P><P>There is a <TTCLASS="FILENAME">stdin</TT>, <TTCLASS="FILENAME">stdout</TT>,        and a <TTCLASS="FILENAME">stderr</TT> associated with each command.        <TTCLASS="USERINPUT"><B>ls 2&#62;&#38;1</B></TT> means temporarily connecting the        <TTCLASS="FILENAME">stderr</TT> of the <BCLASS="COMMAND">ls</B> command to the        same <SPANCLASS="QUOTE">"resource"</SPAN> as the shell's        <TTCLASS="FILENAME">stdout</TT>.</P><P>By convention, a command reads its input from fd 0        (<TTCLASS="FILENAME">stdin</TT>), prints normal output to fd        1 (<TTCLASS="FILENAME">stdout</TT>), and error ouput to fd 2        (<TTCLASS="FILENAME">stderr</TT>). If one of those three fd's is        not open, you may encounter problems:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat /etc/passwd &#62;&#38;-</B></TT> <TTCLASS="COMPUTEROUTPUT">cat: standard output: Bad file descriptor</TT>       </PRE></TD></TR></TABLE><P>For example, when <BCLASS="COMMAND">xterm</B> runs, it first        initializes itself.  Before running the user's shell,        <BCLASS="COMMAND">xterm</B> opens the terminal device        (/dev/pts/&#60;n&#62; or something similar) three times.</P><P>At this point, Bash inherits these three file descriptors,        and each command (child process) run by Bash inherits        them in turn, except when you redirect the command.  <AHREF="io-redirection.html#IOREDIRREF">Redirection</A> means reassigning        one of the file descriptors to another file (or a pipe, or        anything permissible). File descriptors may be reassigned        locally (for a command, a command group, a <AHREF="subshells.html#SUBSHELLSREF">subshell</A>, a <AHREF="redircb.html#REDIRREF">while or if or case or for loop</A>...),        or globally, for the remainder of the shell (using <AHREF="internal.html#EXECREF">exec</A>).</P><P><TTCLASS="USERINPUT"><B>ls &#62; /dev/null</B></TT> means        running <BCLASS="COMMAND">ls</B> with its fd 1 connected to        <TTCLASS="FILENAME">/dev/null</TT>.</P><P>      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>lsof -a -p $$ -d0,1,2</B></TT> <TTCLASS="COMPUTEROUTPUT">COMMAND PID     USER   FD   TYPE DEVICE SIZE NODE NAME bash    363 bozo        0u   CHR  136,1         3 /dev/pts/1 bash    363 bozo        1u   CHR  136,1         3 /dev/pts/1 bash    363 bozo        2u   CHR  136,1         3 /dev/pts/1</TT>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>exec 2&#62; /dev/null</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>lsof -a -p $$ -d0,1,2</B></TT> <TTCLASS="COMPUTEROUTPUT">COMMAND PID     USER   FD   TYPE DEVICE SIZE NODE NAME bash    371 bozo        0u   CHR  136,1         3 /dev/pts/1 bash    371 bozo        1u   CHR  136,1         3 /dev/pts/1 bash    371 bozo        2w   CHR    1,3       120 /dev/null</TT>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>bash -c 'lsof -a -p $$ -d0,1,2' | cat</B></TT> <TTCLASS="COMPUTEROUTPUT">COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME lsof    379 root    0u   CHR  136,1         3 /dev/pts/1 lsof    379 root    1w  FIFO    0,0      7118 pipe lsof    379 root    2u   CHR  136,1         3 /dev/pts/1</TT>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "$(bash -c 'lsof -a -p $$ -d0,1,2' 2&#62;&#38;1)"</B></TT> <TTCLASS="COMPUTEROUTPUT">COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME lsof    426 root    0u   CHR  136,1         3 /dev/pts/1 lsof    426 root    1w  FIFO    0,0      7520 pipe lsof    426 root    2w  FIFO    0,0      7520 pipe</TT></PRE></TD></TR></TABLE></P><P>This works for different types of redirection.</P><P><TTCLASS="USERINPUT"><B>Exercise:</B></TT> Analyze the following script.<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#! /usr/bin/env bash   2&nbsp;   3&nbsp;mkfifo /tmp/fifo1 /tmp/fifo2   4&nbsp;while read a; do echo "FIFO1: $a"; done &#60; /tmp/fifo1 &#38; exec 7&#62; /tmp/fifo1   5&nbsp;exec 8&#62; &#62;(while read a; do echo "FD8: $a, to fd7"; done &#62;&#38;7)   6&nbsp;   7&nbsp;exec 3&#62;&#38;1   8&nbsp;(   9&nbsp; (  10&nbsp;  (  11&nbsp;   while read a; do echo "FIFO2: $a"; done &#60; /tmp/fifo2 | tee /dev/stderr \  12&nbsp;   | tee /dev/fd/4 | tee /dev/fd/5 | tee /dev/fd/6 &#62;&#38;7 &#38; exec 3&#62; /tmp/fifo2  13&nbsp;  14&nbsp;   echo 1st, to stdout  15&nbsp;   sleep 1  16&nbsp;   echo 2nd, to stderr &#62;&#38;2  17&nbsp;   sleep 1  18&nbsp;   echo 3rd, to fd 3 &#62;&#38;3  19&nbsp;   sleep 1  20&nbsp;   echo 4th, to fd 4 &#62;&#38;4  21&nbsp;   sleep 1  22&nbsp;   echo 5th, to fd 5 &#62;&#38;5  23&nbsp;   sleep 1  24&nbsp;   echo 6th, through a pipe | sed 's/.*/PIPE: &#38;, to fd 5/' &#62;&#38;5  25&nbsp;   sleep 1  26&nbsp;   echo 7th, to fd 6 &#62;&#38;6  27&nbsp;   sleep 1  28&nbsp;   echo 8th, to fd 7 &#62;&#38;7  29&nbsp;   sleep 1  30&nbsp;   echo 9th, to fd 8 &#62;&#38;8  31&nbsp;  32&nbsp;  ) 4&#62;&#38;1 &#62;&#38;3 3&#62;&#38;- | while read a; do echo "FD4: $a"; done 1&#62;&#38;3 5&#62;&#38;- 6&#62;&#38;-  33&nbsp; ) 5&#62;&#38;1 &#62;&#38;3 | while read a; do echo "FD5: $a"; done 1&#62;&#38;3 6&#62;&#38;-  34&nbsp;) 6&#62;&#38;1 &#62;&#38;3 | while read a; do echo "FD6: $a"; done 3&#62;&#38;-  35&nbsp;  36&nbsp;rm -f /tmp/fifo1 /tmp/fifo2  37&nbsp;  38&nbsp;  39&nbsp;# For each command and subshell, figure out which fd points to what.  40&nbsp;# Good luck!  41&nbsp;  42&nbsp;exit 0</PRE></TD></TR></TABLE>      </P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="exitcodes.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="command-line-options.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Exit Codes With Special Meanings</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Command-Line Options</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -