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

📄 ch44_23.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.23 Reading Files with the . and source Commands </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:54:09Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch44_01.htm"TITLE="44. Shell Programming for the Uninitiated"><LINKREL="prev"HREF="ch44_22.htm"TITLE="44.22 Finding a Program Name; Multiple Program Names "><LINKREL="next"HREF="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_22.htm"TITLE="44.22 Finding a Program Name; Multiple Program Names "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.22 Finding a Program Name; Multiple Program Names "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 44<BR>Shell Programming for the Uninitiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="CHAPTER"HREF="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45. Shell Programming for the Initiated"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7250">44.23 Reading Files with the . and source Commands </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52083"></A><ACLASS="indexterm"NAME="AUTOID-52086"></A>As article<ACLASS="xref"HREF="ch06_02.htm"TITLE="Parent-Child Relationships ">6.2</A>explains,UNIX programs can <EMCLASS="emphasis">never, ever</EM> modify theenvironmentof their parents. Aprogram can only modify the environment that later will be passed to its children. This is acommon mistake that many new UNIX users make: they try to write aprogram that changes a directory (or does something else involving anenvironment variable) and try to figure out why it doesn't work. Youcan't do this. If you write a program that executes the <EMCLASS="emphasis">cd</EM>command, that <EMCLASS="emphasis">cd</EM> will be effective within your program&nbsp;- but whenthe program finishes, you'll be back in your original (parent) shell.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52094"></A><ACLASS="indexterm"NAME="AUTOID-52096"></A><ACLASS="indexterm"NAME="AUTOID-52099"></A>One workaround is to &quot;source&quot; the shell script file (for <EMCLASS="emphasis">csh</EM> and<EMCLASS="emphasis">bash</EM>) orrun it as a &quot;dot&quot; script (<EMCLASS="emphasis">sh</EM>, <EMCLASS="emphasis">ksh</EM> and <EMCLASS="emphasis">bash</EM> too).For example, if the file named <EMCLASS="emphasis">change-my-directory</EM> contains:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cd /somewhere/else</PRE></BLOCKQUOTE></P><PCLASS="para">you could use the following commands to change the current directoryof the current shell:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>source change-my-directory</B></CODE>$ <CODECLASS="userinput"><B>. change-my-directory</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">source</EM> and<CODECLASS="literal">.</CODE> commandsread a script file into the current shell instead of starting a child shell.These commands only work for shell script files (files containing commandlines as you'd type them at a shell prompt).You can't use <EMCLASS="emphasis">source</EM> or<CODECLASS="literal">.</CODE> to read a binary (directly executable) file into the shell.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52121"></A><ACLASS="indexterm"NAME="AUTOID-52124"></A><ACLASS="indexterm"NAME="AUTOID-52127"></A>If your shell doesn't have<SPANCLASS="link">shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>,you can<SPANCLASS="link">simulate them (<ACLASS="linkend"HREF="ch10_10.htm"TITLE="Simulated Bourne Shell Functions and Aliases ">10.10</A>)</SPAN>with the<CODECLASS="literal">.</CODE> command.It acts a lot like a subroutine or function in a programming language.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</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="ch44_22.htm"TITLE="44.22 Finding a Program Name; Multiple Program Names "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.22 Finding a Program Name; Multiple Program Names "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="CHAPTER"HREF="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45. Shell Programming for the Initiated"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.22 Finding a Program Name; Multiple Program Names </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">45. Shell Programming for the Initiated</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 + -