📄 ch45_11.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.11 The Multipurpose jot Command </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:54:32Z"><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_10.htm"TITLE="45.10 Removing a File Once It's Opened - for Security and Easy Cleanup"><LINKREL="next"HREF="ch45_12.htm"TITLE="45.12 Parameter Substitution "></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_10.htm"TITLE="45.10 Removing a File Once It's Opened - for Security and Easy Cleanup"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.10 Removing a File Once It's Opened - for Security and Easy Cleanup"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_12.htm"TITLE="45.12 Parameter Substitution "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.12 Parameter Substitution "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-9040">45.11 The Multipurpose jot Command </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-9040-IX-BOURNE-SHELL-SH-JOT-COMMAND-USED-IN"></A><ACLASS="indexterm"NAME="UPT-ART-9040-IX-JOT-COMMAND"></A>The <EMCLASS="emphasis">jot</EM> commandis an extremely powerful tool for shell programming.People who have used<EMCLASS="emphasis">jot</EM> end up using it all the time, but those who haven't beenexposed to it may be perplexed by its function.For that reason,I think the best way to learn <EMCLASS="emphasis">jot</EM> is by example.(If you'veread many of my other articles, then you might notice that I think that exampleis the best way to learn anything; but for <EMCLASS="emphasis">jot</EM>, it's doublytrue!)</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="jot">jot</A><BR></TH><TDVALIGN="TOP">In its most basic use, <EMCLASS="emphasis">jot</EM> produces a series of numbers.With only one integer as a command-line argument, it producesthe sequential integers from 1 to that number. </TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 4</B></CODE>1234</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52873"></A><ACLASS="indexterm"NAME="AUTOID-52876"></A><ACLASS="indexterm"NAME="AUTOID-52879"></A>Big deal, you might say.Well, itmay not be earth-shattering on thesurface, but it can make your life much easier if you program inthe Bourne shell.Take the instancewhen you want to increment a number in a loop.The usual way of doing this is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">counter=1while [ $counter -le 10 ]do ... counter=`expr $counter + 1`done</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52885"></A><ACLASS="indexterm"NAME="AUTOID-52888"></A><ACLASS="indexterm"NAME="AUTOID-52891"></A><ACLASS="indexterm"NAME="AUTOID-52894"></A><ACLASS="indexterm"NAME="AUTOID-52897"></A>This is laborious and quite slow.Because the Bourne shelldoesn't have any<SPANCLASS="link">built-in (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>number crunching, the only way to incrementthe counter is to use the <EMCLASS="emphasis">expr</EM> command in each iteration of theloop.But if you have <EMCLASS="emphasis">jot</EM>, the same loop can be writtenin a simple <EMCLASS="emphasis">for</EM> loop:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">for counter in `jot 10`do ...done</PRE></BLOCKQUOTE></P><PCLASS="para"> You can also use <EMCLASS="emphasis">jot</EM> to show any other sequence of numbers.For example, to show the integers between 24 and 28 (inclusive), try:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 5 24</B></CODE>2425262728</PRE></BLOCKQUOTE></P><PCLASS="para">The first argument (<EMCLASS="emphasis">5</EM>) is taken to be how many values shouldbe shown. The second number (<EMCLASS="emphasis">24</EM>) is the number to startcounting with. This may seem frustrating - why not just let us say"jot 24 28" to specify the beginning and end of the sequence, ratherthan make us figure out how many numbers will be in the sequencefirst? Well, the reason is that you might not always want to becounting by whole numbers.</P><PCLASS="para">If supplied with a third argument, <EMCLASS="emphasis">jot</EM> takes it as the number to endthe sequence with. And if you specify either the beginning or endboundary with a decimal point, it will produce fractional numbers inits output:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 5 24 28.0</B></CODE>24.025.026.027.028.0% <CODECLASS="userinput"><B>jot 4 24 28.0</B></CODE>24.025.326.728.0</PRE></BLOCKQUOTE></P><PCLASS="para">You can also use the <EMCLASS="emphasis">-p</EM> option to specify a given precision:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -p4 4 24 28</B></CODE>24.000025.333326.666728.0000</PRE></BLOCKQUOTE></P><PCLASS="para">By default, the values shown are evenly spaced across the interval.You can change this by usinga fourth numerical argument, which becomes thesize of each step in the iteration.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 4 24 28.0 .5</B></CODE>24.024.525.025.5</PRE></BLOCKQUOTE></P><PCLASS="para">Notice in this example that only the first four iterations are shown, because we asked for only four values in the firstargument.This is because any three values determine the fourth automatically,so when the values conflict, <EMCLASS="emphasis">jot</EM> takes the lower value.<EMCLASS="emphasis">jot</EM> stopped after four values regardless of the factthat it would need nine iterations to complete the sequence.However, <EMCLASS="emphasis">jot</EM> will also stopif the sequence is completed <EMCLASS="emphasis">before</EM> the specifiednumber of values are shown.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 4 24 28 2</B></CODE>242628</PRE></BLOCKQUOTE></P><PCLASS="para">To omit any of these values, replace them with a single dash(<CODECLASS="literal">-</CODE>).For example, if you know that you just want thedigits from 24 to 28, you can omit the field specifying the number ofvalues as long as you tell it to use a step of 1:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot - 24 28 1</B></CODE>242526 2728</PRE></BLOCKQUOTE></P><PCLASS="para">And of course, you can use negative numbers and negative steps:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot - 1 -3 -2</B></CODE>1-1-3</PRE></BLOCKQUOTE></P><P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -