📄 ch45_11.htm
字号:
CLASS="para">If you want the output separated by a string other than a newline,use the <EMCLASS="emphasis">-s</EM> option.For example, to have the output of theprevious command separated by spaces, enter:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -s " " - 1 -3 -2</B></CODE>1 -1 -3</PRE></BLOCKQUOTE></P><PCLASS="para">That's <EMCLASS="emphasis">jot</EM> in its no-frills form, already potentially useful forany writer of shell scripts.However, <EMCLASS="emphasis">jot</EM> does quitea lot more.The <EMCLASS="emphasis">-c</EM> option can be used to show<SPANCLASS="link">ASCII (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>)</SPAN>characters instead ofintegers.To print out the character for ASCII 65 (decimal), for example, try:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -c 1 65</B></CODE>A</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52960"></A><ACLASS="indexterm"NAME="AUTOID-52963"></A>You can also do the ASCII-to-decimal conversion in reverse, by justspecifying a character in place of the lower bound:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot 1 A</B></CODE>65</PRE></BLOCKQUOTE></P><PCLASS="para">This can be handy if you want an automatic listing of all 26 letters:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -c 26 A</B></CODE>ABC ...</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52973"></A><ACLASS="indexterm"NAME="AUTOID-52976"></A><ACLASS="indexterm"NAME="AUTOID-52979"></A><ACLASS="indexterm"NAME="AUTOID-52982"></A><ACLASS="indexterm"NAME="AUTOID-52985"></A>The <EMCLASS="emphasis">-r</EM> option produces random numbers, which is very useful inshells with no random number generator (such as the Bourne or Cshells). To create a 6-digit random number, try:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -r 1 100000 999999</B></CODE>523467</PRE></BLOCKQUOTE></P><PCLASS="para"> (Using <EMCLASS="emphasis">-r</EM>, the fourth numerical argument, if specified, istaken to be a seed for the random number.)</P><PCLASS="para">The <EMCLASS="emphasis">-b</EM> option can be used to repeat a given word, much like the<SPANCLASS="link"><EMCLASS="emphasis">yes</EM> (<ACLASS="linkend"HREF="ch23_04.htm"TITLE='Answer "Yes" or "No" Forever with yes'>23.4</A>)</SPAN>command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jot -b lunchtime! 3</B></CODE>lunchtime!lunchtime!lunchtime!</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53002"></A><ACLASS="indexterm"NAME="AUTOID-53005"></A><ACLASS="indexterm"NAME="AUTOID-53008"></A>The <EMCLASS="emphasis">jot</EM> manual page suggests a clever way of using this feature:if you want to search for lines in files that have 40 or morecharacters, you could do this using regular expressions, but you'dhave to count out all those dots.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">grep "........................................" <CODECLASS="replaceable"><I>file</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Using <EMCLASS="emphasis">jot</EM>, you can pat yourself on the back for beingingenious (or for just reading the manpage!):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">grep `jot -s "" -b . 40` <CODECLASS="replaceable"><I>file</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53021"></A>But the most powerful feature of <EMCLASS="emphasis">jot</EM> comes with its <EMCLASS="emphasis">-w</EM>option.The <EMCLASS="emphasis">-w</EM> option accepts a word containing formatconversion characters as used by the <EMCLASS="emphasis">printf( )</EM> function.(For example, <CODECLASS="literal">%d</CODE> prints a decimal argument; <CODECLASS="literal">%h</CODE> printshexadecimal.)If you aren't familiar with the <EMCLASS="emphasis">printf( )</EM> format conversions,read your <EMCLASS="emphasis">printf</EM>(3) manual page or check any C programming book.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53032"></A><ACLASS="indexterm"NAME="AUTOID-53033"></A>This allows you to combine strings with <EMCLASS="emphasis">jot</EM> output, a usefulfeature for manipulating temporary files within scripts. For example, suppose you have a shell script that creates multipletemporary files that you want to remove at the end of the script.You might have even created the files using <EMCLASS="emphasis">jot</EM> earlier in thescript, as shown previously:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">for counter in `jot 10 1`do <CODECLASS="replaceable"><I>whatever commands</I></CODE> > tmp$counterdone</PRE></BLOCKQUOTE></P><PCLASS="para">Then later on you want to remove the files.You could doanother loop, but it's more efficient to just enter:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">rm `jot -w tmp%d 10 1`</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">jot</EM> command expands to the strings <EMCLASS="emphasis">tmp1</EM> through<EMCLASS="emphasis">tmp10</EM>, which are the names of the temporary files createdearlier in the script.<ACLASS="indexterm"NAME="AUTOID-53048"></A><ACLASS="indexterm"NAME="AUTOID-53049"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LM</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_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"><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_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><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.10 Removing a File Once It's Opened - for Security and Easy Cleanup</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.12 Parameter Substitution </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 & 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 + -