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

📄 ch45_05.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.5 A File That Shows Itself... and What #! Does </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:23Z"><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="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><LINKREL="prev"HREF="ch45_04.htm"TITLE="45.4 Fun with #! "><LINKREL="next"HREF="ch45_06.htm"TITLE="45.6 Making Sure Your Script Runs with Bourne Shell, Without #! "></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="ch45_04.htm"TITLE="45.4 Fun with #! "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.4 Fun with #! "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45<BR>Shell Programming for the Initiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_06.htm"TITLE="45.6 Making Sure Your Script Runs with Bourne Shell, Without #! "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.6 Making Sure Your Script Runs with Bourne Shell, Without #! "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0107">45.5 A File That Shows Itself... and What #! Does </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52455"></A><ACLASS="indexterm"NAME="AUTOID-52458"></A><ACLASS="indexterm"NAME="AUTOID-52461"></A><ACLASS="indexterm"NAME="AUTOID-52465"></A>If your UNIX<SPANCLASS="link">understands (<ACLASS="linkend"HREF="ch44_04.htm"TITLE="Testing How Your System Executes Files ">44.4</A>)</SPAN>executable files that start with <CODECLASS="literal">#!</CODE>,you can use this nice trick to make executable files that displaythemselves (or part of themselves).I used this to make a program named <EMCLASS="emphasis">help</EM> on a system that didn'thave any online help.A program like<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>isn't what you want because it'll display the<CODECLASS="literal">#!</CODE> line as well as the message.Watch what happens:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat help</B></CODE>#!/bin/catFor help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...% <CODECLASS="userinput"><B>chmod +x help</B></CODE>% <CODECLASS="userinput"><B>help</B></CODE>#!/bin/catFor help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52480"></A>The trick is to invoke an interpreter that shows all the lines exceptthe line starting with <CODECLASS="literal">#!</CODE>.For example, this file uses<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>and its <EMCLASS="emphasis">d</EM> commandto ignore (&quot;delete&quot;) the first line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat help</B></CODE>#!/bin/sed 1dFor help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...% <CODECLASS="userinput"><B>help</B></CODE>For help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...</PRE></BLOCKQUOTE></P><PCLASS="para">For longer files, try using<SPANCLASS="link"><CODECLASS="literal">more&nbsp;+2</CODE> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>;this file will show itself screenful-by-screenful, starting at line 2:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat help</B></CODE>#!/usr/ucb/more +2For help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52499"></A><ACLASS="indexterm"NAME="AUTOID-52502"></A><ACLASS="indexterm"NAME="AUTOID-52505"></A><ACLASS="indexterm"NAME="AUTOID-52508"></A>You have to give the absolute pathname to the interpreter because thekernel doesn't use your<SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>.<ACLASS="indexterm"NAME="AUTOID-52512"></A>The kernel can pass just one argument to the interpreter.  More thanone argument probably won't work.  In the next example, I try to passtwo arguments to <EMCLASS="emphasis">grep</EM>-but the kernel passes the whole string<CODECLASS="literal">-v&nbsp;#</CODE> as just one argument.  That confuses <EMCLASS="emphasis">grep</EM>, whichcomplains about every character from the space on:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat help</B></CODE>#!/bin/grep -v #For help with UNIX, call the ACS Consulting Hotline at 555-1212.man command               shows the manual for a command   ...% <CODECLASS="userinput"><B>help</B></CODE>grep: illegal option --grep: illegal option -- ^grep: illegal option -- #Usage: grep -hblcnsvi pattern file . . .</PRE></BLOCKQUOTE></P><PCLASS="para">(Remember, there's no shell interpreting the arguments here. The kerneldoes it.)</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="ch45_04.htm"TITLE="45.4 Fun with #! "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.4 Fun with #! "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="ch45_06.htm"TITLE="45.6 Making Sure Your Script Runs with Bourne Shell, Without #! "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.6 Making Sure Your Script Runs with Bourne Shell, Without #! "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.4 Fun with #! </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.6 Making Sure Your Script Runs with Bourne Shell, Without #! </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 + -