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

📄 ch11_11.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 11] 11.11 Picking Up Where You Left Off </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:36:12Z"><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="ch11_01.htm"TITLE="11. The Lessons of History"><LINKREL="prev"HREF="ch11_10.htm"TITLE="11.10 Check Your History First with :p "><LINKREL="next"HREF="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "></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="ch11_10.htm"TITLE="11.10 Check Your History First with :p "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.10 Check Your History First with :p "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 11<BR>The Lessons of History</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.12 Pass History to Another Shell "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7671">11.11 Picking Up Where You Left Off </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12958"></A><ACLASS="indexterm"NAME="AUTOID-12960"></A><ACLASS="indexterm"NAME="AUTOID-12962"></A>If you want your command history to be remembered even when you logout, set the C shell's <EMCLASS="emphasis">savehist</EM><SPANCLASS="link">shell variable (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>to the number of lines of history you want saved.The Korn shell and <EMCLASS="emphasis">bash</EM> save history automatically; you don't needto set a variable.(If you want to change the number of lines saved by <EMCLASS="emphasis">bash</EM>,set its <EMCLASS="emphasis">HISTFILESIZE</EM> environment variable.In the Korn shell, the <EMCLASS="emphasis">HISTSIZE</EM> variable sets the number of commandsavailable to be recalled in your current shell as well the number savedfor other shells.)</P><PCLASS="para">When you log out, the specified number of lines from the <EMCLASS="emphasis">csh</EM> history list will be saved in a file called<ACLASS="indexterm"NAME="AUTOID-12972"></A><EMCLASS="emphasis">.history</EM> in your home directory.<EMCLASS="emphasis">bash</EM> and <EMCLASS="emphasis">ksh</EM> use the filename given in the <EMCLASS="emphasis">HISTFILE</EM>environment variable; by default, <EMCLASS="emphasis">bash</EM> calls the file<EMCLASS="emphasis">.bash_history</EM> and <EMCLASS="emphasis">ksh</EM> uses <EMCLASS="emphasis">.sh_history</EM>.</P><PCLASS="para">On modern windowing systems, this isn't as trivial as it sounds. Onan old-style terminal, people usually started only one main shell, so they could set the history-saving variablein their <EMCLASS="emphasis">.login</EM> or <EMCLASS="emphasis">.profile</EM> fileand have it apply to their login shell.</P><PCLASS="para">However, under window systems like X or networked filesystems that shareyour home directory between several hosts, you have multiple shellssaving into the same history file.The sections below give some possible fixes.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7671-SECT-1.1">11.11.1 bash and ksh </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12989"></A><ACLASS="indexterm"NAME="AUTOID-12992"></A>Here's the basic way to give a separate history file to each Korn shell or<EMCLASS="emphasis">bash</EM>:Customize your<SPANCLASS="link">setup file (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>to set a different <EMCLASS="emphasis">HISTFILE</EM> on each host or each window.Use names like<EMCLASS="emphasis">$HOME/.sh_history.window</EM><CODECLASS="replaceable"><I>n</I></CODE> or<EMCLASS="emphasis">~/.bash_history.</EM><CODECLASS="replaceable"><I>hostname</I></CODE>to match each file to its window or host.If your setup is always the same each time you log in, that should giveeach window and/or host the same history it had on the last invocation.(There are related tips in articles<ACLASS="xref"HREF="ch02_12.htm"TITLE="Automatic Setups for Different Terminals ">2.12</A>and<ACLASS="xref"HREF="ch02_13.htm"TITLE="A .cshrc.$HOST File for Per Host Setup ">2.13</A>.)</P><PCLASS="para">If you open random windows, though, you'll have a harder timeautomatically matching a history file to a shell the next time youlog in.Cook up your own scheme.</P><PCLASS="para">The simplest fix is to use<SPANCLASS="link"><CODECLASS="literal">$$</CODE> (<ACLASS="linkend"HREF="ch08_19.htm"TITLE='"Special&quot; Characters and Operators '>8.19</A>)</SPAN>-whichwill probably expand differently in almost every shell you ever start&nbsp;- asa unique part of the filename.Here are two possibilities:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">HISTFILE=/tmp/sh_hist.$$HISTFILE=$HOME/.sh_hist.$$</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13011"></A>The first example uses the system's temporary-file directory; article<ACLASS="xref"HREF="ch21_03.htm"TITLE="Unique Names for Temporary Files ">21.3</A>explains and shows one way to clean up.If your system's <EMCLASS="emphasis">/tmp</EM> is cleaned out often, you may be able toleave your history files there and let the system remove them;ask the administrator.Note that the history file may be<SPANCLASS="link">world-readable (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>if your<SPANCLASS="link">umask (<ACLASS="linkend"HREF="ch22_04.htm"TITLE="Setting an Exact umask ">22.4</A>)</SPAN>isn't set to protect your files.If that matters to you, you could make the temporary files in your homedirectory (or some protected directory), as in the second example shown earlier.Article<ACLASS="xref"HREF="ch03_04.htm"TITLE="Automatic File Cleanup ">3.4</A>shows a way to delete those files.</P><PCLASS="para">Two more bits of trivia:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">The Korn shell maintains the history file constantly, adding a new lineto it as soon as you run a command.This means you share history between all your current shells with thesame <EMCLASS="emphasis">HISTFILE</EM> name,which can be a benefit or a problem.</P></LI><LICLASS="listitem"><PCLASS="para">In <EMCLASS="emphasis">bash</EM>, each shell process keeps its own history listin memory. History isn't written to the history file (named bythe <EMCLASS="emphasis">HISTFILE</EM> variable in each shell) until the shell exits.</P><PCLASS="para">You can force a write with the command <EMCLASS="emphasis">history -w</EM>.In the same way, if you have an existing history file (or, actually, anyfile full of command lines), you can read it into your current<EMCLASS="emphasis">bash</EM> with <EMCLASS="emphasis">history -r</EM>. Article<ACLASS="xref"HREF="ch11_12.htm"TITLE="Pass History to Another Shell ">11.12</A> has another example.</P></LI></UL></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7671-SECT-1.2">11.11.2 C Shell </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13035"></A><ACLASS="indexterm"NAME="AUTOID-13038"></A>The C shell has only one possible filename for its automatic historyfile: <EMCLASS="emphasis">.history</EM>.If you set the C shell variable<EMCLASS="emphasis">savehist</EM> in each of your windows (e.g., by setting it in your<EMCLASS="emphasis">.cshrc</EM>), they will all try to write <EMCLASS="emphasis">.history</EM> at once,leading to trouble.And even if that weren't true, you get thehistory from every window or host, which might not be what you want.</P><PCLASS="para">Of course, you could set <EMCLASS="emphasis">savehist</EM> manually in a single window when you thought you might be doing work you might want to pick uplater. But there is another way:use the C shell's command <EMCLASS="emphasis">history -h</EM> (which prints the history listwithout leading numbers, so it can be read back in later) and redirectthe output to a file. Then use <EMCLASS="emphasis">source -h</EM> to read it back intoyour history list when you log in.</P><PCLASS="para">Do you want to automate this?First, you'll need to choose a system of filenames, like<EMCLASS="emphasis">~/.history.window</EM><CODECLASS="replaceable"><I>n</I></CODE> or <EMCLASS="emphasis">~/.history.</EM><CODECLASS="replaceable"><I>hostname</I></CODE>,to match each file to its window or host.If each of your C shells is a<SPANCLASS="link">login shell\** (<ACLASS="linkend"HREF="ch02_08.htm"TITLE="Identifying Login Shells ">2.8</A>)</SPAN>,[1]you can run <EMCLASS="emphasis">history -h</EM> from your <EMCLASS="emphasis">.logout</EM> file and<EMCLASS="emphasis">source -h</EM> from your <EMCLASS="emphasis">.login</EM> file.For non-login shells, automation is tougher&nbsp;- try this:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] In the<SPANCLASS="link">X Window System (<ACLASS="linkend"HREF="ch01_31.htm"TITLE="The X Window System ">1.31</A>)</SPAN>,set the <EMCLASS="emphasis">xterm&nbsp;-ls</EM> optionto force a login shell.</P></BLOCKQUOTE><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Set the<SPANCLASS="link"><EMCLASS="emphasis">ignoreeof</EM> variable (<ACLASS="linkend"HREF="ch03_05.htm"TITLE="Stop Accidental C Shell Logouts ">3.5</A>)</SPAN>to force you to leave the shell with an<SPANCLASS="link"><EMCLASS="emphasis">exit</EM> (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>command.</P></LI><LICLASS="listitem"><PCLASS="para">Set an<SPANCLASS="link">alias for <EMCLASS="emphasis">exit</EM> (<ACLASS="linkend"HREF="ch10_06.htm"TITLE="Avoiding C Shell Alias Loops ">10.6</A>)</SPAN>that runs <EMCLASS="emphasis">history -h</EM> before exiting.&#13;</P></LI><LICLASS="listitem"><PCLASS="para">Run <EMCLASS="emphasis">source -h</EM> from your<EMCLASS="emphasis">.cshrc</EM> file.Use a<SPANCLASS="link"><CODECLASS="literal">$?prompt</CODE> test (<ACLASS="linkend"HREF="ch02_09.htm"TITLE="Speeding Up Your C Shell with set prompt Test ">2.9</A>)</SPAN>to be sure this only runs in interactive shells.</P></LI></UL><PCLASS="para">If you choose to run <EMCLASS="emphasis">history -h</EM> and <EMCLASS="emphasis">source -h</EM> by handoccasionally, they will allow you the kind of control you need to<SPANCLASS="link">write a script (<ACLASS="linkend"HREF="ch11_12.htm"TITLE="Pass History to Another Shell ">11.12</A>)</SPAN>that saves and restores only what you want.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">TOR</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="ch11_10.htm"TITLE="11.10 Check Your History First with :p "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.10 Check Your History First with :p "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="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.12 Pass History to Another Shell "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">11.10 Check Your History First with :p </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">11.12 Pass History to Another Shell </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 + -