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

📄 ch38_03.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
></A>When you're typing commands at the shell, it is the <BCLASS="emphasis.bold">controllingprocess</B> of your terminal, meaning that it (the shell) is the process thatgets the input you type.See article<ACLASS="xref"HREF="ch38_06.htm"TITLE="The Controlling Terminal ">38.6</A>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42329"></A><ACLASS="indexterm"NAME="AUTOID-42332"></A><ACLASS="indexterm"NAME="AUTOID-42335"></A><ACLASS="indexterm"NAME="AUTOID-42338"></A><ACLASS="indexterm"NAME="AUTOID-42340"></A>Normally, when you type a command at the shell prompt, that command runs andis allowed by the shell to take over the terminal for its lifetime.For example, if you type <CODECLASS="literal">more</CODE> <CODECLASS="literal">.login</CODE> to view your <EMCLASS="emphasis">.login</EM>file, the shell starts up the<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>program and then sits around waitingfor it to finish; while <EMCLASS="emphasis">more</EM> is running, you can type commands topage through the file and <EMCLASS="emphasis">more</EM> (not the shell) will see them.The command you run is called a <BCLASS="emphasis.bold">child</B> or <BCLASS="emphasis.bold">subprocess</B> of theshell process, which is its <BCLASS="emphasis.bold">parent</B>.All process information (user ID, group ID, etc.) is inherited by the childfrom its parent, except for the process ID, since the child is assigned anew one.[<SPANCLASS="link">Built-in shell commands (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>like <EMCLASS="emphasis">cd</EM> don't start a child process. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42357"></A>Although the normal behavior is for the shell to wait until anycommand you run has finished before it becomes active again, there aresome situations in which you don't want this to occur. For example,if you're using a window system such as<SPANCLASS="link">X (<ACLASS="linkend"HREF="ch01_31.htm"TITLE="The X Window System ">1.31</A>)</SPAN>andwant to start up a new <EMCLASS="emphasis">xterm</EM> window from your shell, you don'twant to type just <CODECLASS="literal">xterm</CODE>, because then your original shell willwait until the <EMCLASS="emphasis">xterm</EM> finishes before allowing you to type any morecommands. This would mean that you still have only one shell to workin, thus defeating the purpose of starting the new <EMCLASS="emphasis">xterm</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42366"></A><ACLASS="indexterm"NAME="AUTOID-42368"></A><ACLASS="indexterm"NAME="AUTOID-42371"></A><ACLASS="indexterm"NAME="AUTOID-42374"></A><ACLASS="indexterm"NAME="AUTOID-42377"></A><ACLASS="indexterm"NAME="AUTOID-42379"></A>When you don't want a process to finish before getting back to theshell, you can run it in the<SPANCLASS="link"><BCLASS="emphasis.bold">background</B> (<ACLASS="linkend"HREF="ch01_26.htm"TITLE="What a Multiuser System Can Do for You ">1.26</A>)</SPAN>.You do this by puttingan ampersand (<CODECLASS="literal">&amp;</CODE>) character at the end of the command, for example,<CODECLASS="literal">xterm</CODE> <CODECLASS="literal">&amp;</CODE>. The shell will start the child process and thenimmediatelyprompt you for another command. Note that in this situation, theshell retains control of the terminal and the newly created backgroundprocess cannot read input. Some shells have additional<SPANCLASS="link"><BCLASS="emphasis.bold">job control</B> (<ACLASS="linkend"HREF="ch12_08.htm"TITLE="Job Control in a Nutshell ">12.8</A>)</SPAN>features (processes that are running in the background areoften described as <BCLASS="emphasis.bold">background jobs</B> or just jobs) that enableyou to do things such as kill jobs or bring a job from the backgroundinto the <BCLASS="emphasis.bold">foreground</B> so that it becomes the controlling process ofthe terminal and you can type input at it.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42391"></A>An important thing to remember is that although process information isinherited by children <EMCLASS="emphasis">when they are started</EM>, it is impossible forthe parent to affect its child's process information (or vice versa)after that point. For example, if you<SPANCLASS="link">start up the editor <EMCLASS="emphasis">vi</EM>, suspend it (<ACLASS="linkend"HREF="ch12_04.htm"TITLE="Job Control and autowrite: Real Time Savers! ">12.4</A>)</SPAN>,and then use the <EMCLASS="emphasis">cd</EM> command in the shell to change directories,<EMCLASS="emphasis">vi</EM> will still have the old working directory when you bring itback into the foreground.  Similarly, if you write a shell script thatchanges some environment variables, those variables will contain theirold values in the shell when the shell script exits. This sometimesconfuses MS-DOS users, since information such as the currentdirectory is stored in a global area which is referenced by allprograms. If it is necessary to communicate information from a childback to a parent shell,<SPANCLASS="link"> other methods are needed . (<ACLASS="linkend"HREF="ch38_08.htm"TITLE="What Are Signals? ">38.8</A>, <ACLASS="linkend"HREF="ch44_23.htm"TITLE="Reading Files with the . and source Commands ">44.23</A>)</SPAN></P><PCLASS="para">[One more concept that I think is useful: When a process exits, itreturns a numeric<SPANCLASS="link">exit status (<ACLASS="linkend"HREF="ch44_07.htm"TITLE="Exit Status of UNIX Processes ">44.7</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-42402"></A><ACLASS="indexterm"NAME="AUTOID-42404"></A><ACLASS="indexterm"NAME="AUTOID-42407"></A><ACLASS="indexterm"NAME="AUTOID-42410"></A><ACLASS="indexterm"NAME="AUTOID-42413"></A>to its parent process.By convention, a zero status means success; non-zero means some kindof failure. <EMCLASS="emphasis">-JP</EM>]</P><PCLASS="para">Just as there are ways to modify the environment and the currentworking directory of the shell, there are also useful ways to<SPANCLASS="link">manipulate file descriptors (<ACLASS="linkend"HREF="ch45_20.htm"TITLE="Overview: Open Files and File Descriptors ">45.20</A>, <ACLASS="linkend"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>, <ACLASS="linkend"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>)</SPAN>.<ACLASS="indexterm"NAME="AUTOID-42419"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JIK</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_02.htm"TITLE="38.2 fork and exec "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.2 fork and exec "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_04.htm"TITLE="38.4 Subshells "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.4 Subshells "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">38.2 fork and exec </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">38.4 Subshells </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

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