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

📄 ch10_05.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 10] 10.5 Sourceable Scripts </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:35:49Z"><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="ch10_01.htm"TITLE="10. Aliases"><LINKREL="prev"HREF="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "><LINKREL="next"HREF="ch10_06.htm"TITLE="10.6 Avoiding C Shell Alias Loops "></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="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.4 Aliases in ksh and bash "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 10<BR>Aliases</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch10_06.htm"TITLE="10.6 Avoiding C Shell Alias Loops "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.6 Avoiding C Shell Alias Loops "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5600">10.5 Sourceable Scripts </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-5600-IX-SOURCEABLE-SCRIPTS-DESCRIBED"></A><ACLASS="indexterm"NAME="UPT-ART-5600-IX-SCRIPTS-SOURCEABLE"></A>A powerful concept in <EMCLASS="emphasis">csh</EM> is that of aliases. Another greatcapability is shell scripts. Each has its strengths. An alias is justright for common sequences of commands, calling a command by a differentname, and so on.Scripts are great for more flexible processing and batch processing.There are limitations to both, and I will show a way around them.</P><PCLASS="para">The limitation to aliases is that you are working pretty much withone command line. Consider this example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias pp 'set o2=$cwd; popd; set old=$o2; dir_number; record_dir pp; \\  prompt_set; set cd_attempt=(\!*); if ($#cd_attempt &gt; 0) cd $cd_attempt'</PRE></BLOCKQUOTE></P><PCLASS="para">Now this works fine for me, and it served me well for a few yearsand thousands of invocations, but it's at the point where I start thinking thata script is more suited to the job. This brings me to the limitationof scripts...</P><PCLASS="para">Shell scripts are great for accomplishing some task that might changea file, start a program, etc. They are limited by the fact thatany changes they make to shell or environment variables<SPANCLASS="link">are not visible (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>to the parent shell that started them.In other words, you can write somereally cool script that will change directories for you if you don'ttouch the keyboard for five seconds, but once the script exits, you arestill in the same place you started.</P><PCLASS="para">The answer is to combine the best of both worlds. Consider this:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch47_05.htm"TITLE="47.5 Using C Shell Arrays ">( )</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias pp 'set cd_attempt=(\!*); source ~/bin/pp_csh'</PRE></TD></TR></TABLE></P><PCLASS="para">We set up a variable and source a script. The conceptis this: put your command-line arguments into a variable and then<SPANCLASS="link"><EMCLASS="emphasis">source</EM> (<ACLASS="linkend"HREF="ch44_23.htm"TITLE="Reading Files with the . and source Commands ">44.23</A>)</SPAN>a script in order to accomplish something. The difference here is thatbecause you are not starting a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>for the script, it can do everythingan alias can and more. This is much like<SPANCLASS="link">Bourne shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>.</P><PCLASS="para">Some hints on using this technique:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">Naming:</B>I like to name the script that is doing all of thework after the alias, with <EMCLASS="emphasis">_csh</EM> or <EMCLASS="emphasis">.csh</EM> at the end of itsname. I put all of the scripts in my<SPANCLASS="link"><EMCLASS="emphasis">~/bin</EM> (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>.[Instead of names ending in <EMCLASS="emphasis">.csh</EM>, I put mine in a directory named<EMCLASS="emphasis">~/.lib/csh</EM>. <EMCLASS="emphasis">-JP</EM>&nbsp;]</P></LI><LICLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">Feedback:</B>You don't want to execute the script directly. You wantto source it.Here's a good first line that detects this:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch45_05.htm"TITLE="45.5 A File That Shows Itself... and What #! Does ">#!</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">#! /bin/echo sorry,try:source</PRE></TD></TR></TABLE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">Usage statement:</B>Check the variable that you expect to see from the alias.If it isn't there, you can show a usage statement, anddo a <EMCLASS="emphasis">goto</EM> to the end of the script:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch47_04.htm"TITLE="47.4 C Shell Variable Operators and Expressions ">$#</A> <ACLASS="co"HREF="ch08_18.htm"TITLE="8.18 Here Documents ">&lt;&lt;</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">if ($#lg_args == 0) then1usage: lg [-a][-p] pattern [command]    -a  lists all (.dot files)    -p  pipe resulting list into command+++    goto lg_endendif   ...lg_end:</PRE></TD></TR></TABLE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">Alias options:</B>You aren't limited to what an alias can do, since you aresourcing a script. You gain some flexibility here. Here's one way ofhandling options:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch47_06.htm"TITLE="47.6 Quick Reference: C Shell switch Statement ">switch</A> <ACLASS="co"HREF="ch47_05.htm"TITLE="47.5 Using C Shell Arrays ">[1]</A> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">set</A> <ACLASS="co"HREF="ch47_05.htm"TITLE="47.5 Using C Shell Arrays ">shift</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">unset ls_argwhile (! $?ls_arg)    switch (&quot;$lg_args[1]&quot;)        case &quot;-a&quot;:            set ls_arg=&quot;-a&quot;            shift lg_args        case &quot;-p&quot;:            set use_pipe            shift lg_args        default:            set ls_arg            breaksw    endswend</PRE></TD></TR></TABLE></P></LI></UL><PCLASS="para">Have fun with this! You may find yourself tossing some old aliasesand rewriting them as sourceable scripts. They're also easier to maintain.<ACLASS="indexterm"NAME="AUTOID-11997"></A><ACLASS="indexterm"NAME="AUTOID-11998"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DS</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="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.4 Aliases in ksh and bash "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="ch10_06.htm"TITLE="10.6 Avoiding C Shell Alias Loops "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.6 Avoiding C Shell Alias Loops "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">10.4 Aliases in ksh and bash </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">10.6 Avoiding C Shell Alias Loops </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 + -