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

📄 ch07_12.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 7] 7.12 External Commands Send Signals to Set Variables </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:59Z"><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_11.htm"TITLE="7.11 dirs in Your Prompt: Better than $cwd "><LINKREL="next"HREF="ch07_13.htm"TITLE="7.13 Pre-Prompt Commands in bash "></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_11.htm"TITLE="7.11 dirs in Your Prompt: Better than $cwd "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.11 dirs in Your Prompt: Better than $cwd "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_13.htm"TITLE="7.13 Pre-Prompt Commands in bash "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.13 Pre-Prompt Commands in bash "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7540">7.12 External Commands Send Signals to Set Variables </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7266"></A><ACLASS="indexterm"NAME="AUTOID-7268"></A><ACLASS="indexterm"NAME="AUTOID-7271"></A>The Bourne shell's<SPANCLASS="link"><EMCLASS="emphasis">trap</EM> (<ACLASS="linkend"HREF="ch44_12.htm"TITLE="Trapping Exits Caused by Interrupts ">44.12</A>)</SPAN>will run one or more commands when the shell gets a<SPANCLASS="link">signal (<ACLASS="linkend"HREF="ch38_08.htm"TITLE="What Are Signals? ">38.8</A>)</SPAN>(usually, from the <EMCLASS="emphasis">kill</EM> command).The shell will run any command, including commands thatset shell variables.For instance, the shell could re-read a configuration file; article<ACLASS="xref"HREF="ch38_11.htm"TITLE="Printer Queue Watcher: A Restartable Daemon Shell Script">38.11</A>shows that.Or it could set a new <EMCLASS="emphasis">PS1</EM> prompt variable that's updated any timean external command (like another shell script or a<SPANCLASS="link"><EMCLASS="emphasis">cron</EM> job (<ACLASS="linkend"HREF="ch40_12.htm"TITLE="Periodic Program Execution: The cron Facility ">40.12</A>)</SPAN>)sends the shell a signal.There are lots of possibilities.</P><PCLASS="para">This trick takes over signal 5, which usually isn't used.When the shell gets signal 5, a <EMCLASS="emphasis">trap</EM> runs a command to get thedate and time, then resets the prompt.A<SPANCLASS="link">background (<ACLASS="linkend"HREF="ch01_27.htm"TITLE="How Background Processing Works ">1.27</A>)</SPAN>job springs this trap once a minute.So, every minute, after you type any command, your prompt will change.</P><PCLASS="para">You could run any command: count the number of users, show the<SPANCLASS="link">load average (<ACLASS="linkend"HREF="ch39_07.htm"TITLE="Checking System Load: uptime ">39.7</A>)</SPAN>,whatever.And newer shells, like <EMCLASS="emphasis">bash</EM>, can run a command<SPANCLASS="link">in backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>each time the prompt is displayed&nbsp;- article<ACLASS="xref"HREF="ch07_08.htm"TITLE="Highlighting in Shell Prompts ">7.8</A>has an example.But, to have an external command update a shell variable at any randomtime, this <EMCLASS="emphasis">trap</EM> trick is still the best.</P><PCLASS="para">Now on to the specific example of putting date and time in the oldBourne shell's prompt.<ACLASS="indexterm"NAME="AUTOID-7292"></A><ACLASS="indexterm"NAME="AUTOID-7294"></A>If your system's <EMCLASS="emphasis">date</EM> command doesn't understand date formats(like <CODECLASS="literal">+%a</CODE>), get one that does&nbsp;- like the<SPANCLASS="link">version on the CD-ROM (<ACLASS="linkend"HREF="ch51_10.htm"TITLE="The date Command ">51.10</A>)</SPAN>.Put these lines in your<ACLASS="indexterm"NAME="AUTOID-7300"></A><EMCLASS="emphasis">.profile</EM>file (or just type them in at a Bourne shell prompt):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">&#13;# Put date and time in prompt; update every 60 seconds:trap 'PS1=`date &quot;+%a %D %H:%M%n&quot;`\$\ ' 5while :do    sleep 60    kill -5 $$done &amp;promptpid=$!</PRE></BLOCKQUOTE></P><PCLASS="para">Now, every minute after you type a command, your prompt will change:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">Mon 02/17/92 08:59$ <CODECLASS="userinput"><B>cc bigprog.c</B></CODE>undefined symbol                first referenced in filexputc                               bigprog.old fatal: Symbol referencing errors.Mon 02/17/92 08:59$ <CODECLASS="userinput"><B>ls</B></CODE>bigprog.cbigprog.oMon 02/17/92 09:00$</PRE></BLOCKQUOTE></P><PCLASS="para">The prompt format is up to you.This example makes a<SPANCLASS="link">two-line prompt (<ACLASS="linkend"HREF="ch07_05.htm"TITLE="Multiline Shell Prompts ">7.5</A>)</SPAN>,<ACLASS="indexterm"NAME="AUTOID-7314"></A>with backslashes (<CODECLASS="literal">\</CODE>) to protect the newline and space from the<EMCLASS="emphasis">trap</EM>; a single-line prompt might be easier to design.The manual page for<EMCLASS="emphasis">date</EM>lists what you can put in the prompt.</P><PCLASS="para">This setup starts a<SPANCLASS="link"><EMCLASS="emphasis">while</EM> loop (<ACLASS="linkend"HREF="ch44_10.htm"TITLE="Loops That Test Exit Status ">44.10</A>)</SPAN>in thebackground.<ACLASS="indexterm"NAME="AUTOID-7323"></A>The <EMCLASS="emphasis">promptpid</EM> variable holds the<SPANCLASS="link">process ID number (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>of the background shell.Before you log out, you should<ACLASS="indexterm"NAME="AUTOID-7327"></A><SPANCLASS="link"><EMCLASS="emphasis">kill</EM> (<ACLASS="linkend"HREF="ch38_10.htm"TITLE="Destroying Processes with kill ">38.10</A>)</SPAN>the loop.You can type the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">kill $promptpid</PRE></BLOCKQUOTE></P><PCLASS="para">at a prompt or put it in a file that's<SPANCLASS="link">executed when you log out (<ACLASS="linkend"HREF="ch03_02.htm"TITLE="Running Commands at Bourne/Korn Shell Logout ">3.2</A>)</SPAN>.</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_11.htm"TITLE="7.11 dirs in Your Prompt: Better than $cwd "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.11 dirs in Your Prompt: Better than $cwd "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_13.htm"TITLE="7.13 Pre-Prompt Commands in bash "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.13 Pre-Prompt Commands in bash "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">7.11 dirs in Your Prompt: Better than $cwd </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.13 Pre-Prompt Commands in bash </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 + -