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

📄 ch07_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 7] 7.4 Faster Prompt Setting with Built-Ins </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:33:44Z"><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="ch07_01.htm"TITLE="7. Setting Your Shell Prompt"><LINKREL="prev"HREF="ch07_03.htm"TITLE="7.3 C Shell Prompt Causes Problems in vi, rsh, etc. "><LINKREL="next"HREF="ch07_05.htm"TITLE="7.5 Multiline Shell Prompts "></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="ch07_03.htm"TITLE="7.3 C Shell Prompt Causes Problems in vi, rsh, etc. "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.3 C Shell Prompt Causes Problems in vi, rsh, etc. "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 7<BR>Setting Your Shell Prompt</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch07_05.htm"TITLE="7.5 Multiline Shell Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.5 Multiline Shell Prompts "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1270">7.4 Faster Prompt Setting with Built-Ins </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6713"></A><ACLASS="indexterm"NAME="AUTOID-6716"></A>To set your prompt, you execute a command (on most shells, the commandsets a shell variable).Before setting the prompt, you may run other commands to get informationfor it: the current directory name, for example.A shell can run two kinds of commands:<ACLASS="indexterm"NAME="AUTOID-6718"></A><SPANCLASS="link">built-in and external (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>.Built-in commands usually run faster than external commands.On a slow computer, the difference may be important&nbsp;- waiting a fewseconds for your prompt to reset can get irritating.Creative use of your shell's built-in commands might pay off there.Let's look at some examples:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6724"></A><EMCLASS="emphasis">pwd</EM> is an external command that<SPANCLASS="link">searches the filesystem (<ACLASS="linkend"HREF="ch14_04.htm"TITLE="How Does UNIX Find Your Current Directory? ">14.4</A>)</SPAN>to find your current directory name.(<EMCLASS="emphasis">pwd</EM> is built into some shells, but that version doesn't search thefilesystem.) However, some shells can give you the current directory name from a variable, usually<CODECLASS="literal">$cwd</CODE> or <CODECLASS="literal">$PWD</CODE>.On slow computers, the first prompt-setting command below would takemore time:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">set prompt=&quot;`pwd`% &quot;set prompt=&quot;${cwd}% &quot;</PRE></TD></TR></TABLE></P><PCLASS="para">There's a tradeoff here, though&nbsp;- the shell built-in<SPANCLASS="link">may not (<ACLASS="linkend"HREF="ch14_13.htm"TITLE="Which Directory Am I in, Really? ">14.13</A>)</SPAN>give the right answer.Also, in the C shell, each time you change to a new directory, you needto run a new <CODECLASS="literal">set&nbsp;prompt</CODE> command; you can use an alias like<SPANCLASS="link">setprompt (<ACLASS="linkend"HREF="ch07_05.htm"TITLE="Multiline Shell Prompts ">7.5</A>)</SPAN>to do this automatically.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6740"></A>If you're putting your current directory in your prompt, you may onlywant the tail of the pathname (the name past the last slash).How can you edit a pathname?Most people think of<SPANCLASS="link"><EMCLASS="emphasis">basename</EM> (<ACLASS="linkend"HREF="ch45_18.htm"TITLE="Using basename and dirname ">45.18</A>)</SPAN>or<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>.Using the current directory from <CODECLASS="literal">$cwd</CODE>, they might type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set prompt=&quot;`basename $cwd`% &quot;</PRE></BLOCKQUOTE></P><PCLASS="para">The faster way is with the C shell's built-in &quot;tail&quot; operator, <CODECLASS="literal">:t</CODE>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">{}</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">set prompt=&quot;${cwd:t}% &quot;</PRE></TD></TR></TABLE></P><PCLASS="para">If your current directory is <EMCLASS="emphasis">/usr/users/hanna/projects</EM>, either ofthose prompts would look like this (with a space after the percent sign):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">projects%</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6760"></A>The C shell has several of these built-in<SPANCLASS="link">string operators (<ACLASS="linkend"HREF="ch09_06.htm"TITLE="String Editing (Colon) Operators ">9.6</A>)</SPAN>like <CODECLASS="literal">:t</CODE>;the Korn Shell and <EMCLASS="emphasis">bash</EM> havemore-powerful<SPANCLASS="link">string operators (<ACLASS="linkend"HREF="ch09_07.htm"TITLE="String Editing in ksh and bash ">9.7</A>)</SPAN>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6769"></A><ACLASS="indexterm"NAME="AUTOID-6772"></A>The Korn shell and <EMCLASS="emphasis">bash</EM> can include the current value of anothershell variable in their prompt.So, put<SPANCLASS="link"><CODECLASS="literal">$PWD</CODE> (<ACLASS="linkend"HREF="ch06_03.htm"TITLE="Predefined Environment Variables ">6.3</A>)</SPAN>in your prompt string (the <EMCLASS="emphasis">PS1</EM> shell variable)and the prompt will always show the current directory.Or use any other variable; anytime it changes, the prompt will change too.The important trick is to store the prompt with<ACLASS="indexterm"NAME="AUTOID-6779"></A>single quotes (<CODECLASS="literal">'</CODE>), not double quotes (<CODECLASS="literal">&quot;</CODE>), so thatthe variable name in your prompt won't be<SPANCLASS="link">evaluated (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>, <ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>until the prompt is actually printed to the screen.</P><PCLASS="para">For example, I'll put the current directory and the value of a variablenamed <EMCLASS="emphasis">PSX</EM> in my prompt.When I change either, the prompt changes too:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>PSX=foo</B></CODE>$ <CODECLASS="userinput"><B>PS1='$PWD $PSX\$ '</B></CODE>/home/jerry foo$ <CODECLASS="userinput"><B>PSX=bar</B></CODE>/home/jerry bar$ <CODECLASS="userinput"><B>cd ..</B></CODE>/home bar$</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6796"></A><EMCLASS="emphasis">tcsh</EM> and <EMCLASS="emphasis">bash</EM> also have special prompt string customizationsthat let you include the hostname, username, time, and more.You don't need external UNIX commands; you don't need to use an aliaslike <EMCLASS="emphasis">setprompt</EM> to reset your prompt string.</P><PCLASS="para">For example, to make your shell prompt show your current directory, a newline character (to move to the nextline of a<SPANCLASS="link">two-line prompt (<ACLASS="linkend"HREF="ch07_05.htm"TITLE="Multiline Shell Prompts ">7.5</A>)</SPAN>),then the current time, and finally a <CODECLASS="literal">$</CODE> or <CODECLASS="literal">%</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">PS1='$PWD\n\t \$ '            <ICLASS="lineannotation">...bash</I>set prompt = '%~\\            <ICLASS="lineannotation">...tcsh</I>%p%% '</PRE></BLOCKQUOTE></P><PCLASS="para">For more information, see O'Reilly &amp; Associates'<EMCLASS="emphasis">Using csh &amp; tcsh</EM> and<EMCLASS="emphasis">Learning the bash Shell</EM>-or your shell's manpage.</P></LI></OL><PCLASS="para">So, if your prompt takes too long to set, look for built-ins that cansave time.As another example, article <ACLASS="xref"HREF="ch07_11.htm"TITLE="dirs in Your Prompt: Better than $cwd ">7.11</A>shows how to use <EMCLASS="emphasis">dirs</EM> in a shell prompt.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <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="ch07_03.htm"TITLE="7.3 C Shell Prompt Causes Problems in vi, rsh, etc. "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.3 C Shell Prompt Causes Problems in vi, rsh, etc. "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="ch07_05.htm"TITLE="7.5 Multiline Shell Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.5 Multiline Shell Prompts "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">7.3 C Shell Prompt Causes Problems in vi, rsh, etc. </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">7.5 Multiline Shell Prompts </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 + -