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

📄 extmisc.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 4 页
字号:
><B>yes different	      string</B></TT>, which would continually output	      <TTCLASS="COMPUTEROUTPUT">different string</TT> to	      <TTCLASS="FILENAME">stdout</TT>.</P><P>One might well ask the purpose of this. From the	      command line or in a script, the output of	      <BCLASS="COMMAND">yes</B> can be redirected or piped into a	      program expecting user input. In effect, this becomes a sort	      of poor man's version of <BCLASS="COMMAND">expect</B>.</P><P><TTCLASS="USERINPUT"><B>yes | fsck /dev/hda1</B></TT> runs	      <BCLASS="COMMAND">fsck</B> non-interactively (careful!).</P><P><TTCLASS="USERINPUT"><B>yes | rm -r dirname</B></TT> has same effect as	     <TTCLASS="USERINPUT"><B>rm -rf dirname</B></TT> (careful!).</P><DIVCLASS="WARNING"><TABLECLASS="WARNING"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/warning.png"HSPACE="5"ALT="Warning"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Caution advised when piping <BCLASS="COMMAND">yes</B>	     to a potentially dangerous system command, such	     as <AHREF="system.html#FSCKREF">fsck</A> or <AHREF="system.html#FDISKREF">fdisk</A>. It might have unintended	     consequences.</P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">yes</B> command parses variables.	       For example:</P><P>	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>yes $BASH_VERSION</B></TT> <TTCLASS="COMPUTEROUTPUT">3.1.17(1)-release 3.1.17(1)-release 3.1.17(1)-release 3.1.17(1)-release 3.1.17(1)-release . . .</TT> 	      </PRE></TD></TR></TABLE>	        </P><P>This <SPANCLASS="QUOTE">"feature"</SPAN> may not be particularly useful.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="BANNERREF"></A><BCLASS="COMMAND">banner</B></DT><DD><P>Prints arguments as a large vertical banner to	      <TTCLASS="FILENAME">stdout</TT>, using an ASCII character	      (default '#'). This may be redirected to a printer for	      hardcopy.</P></DD><DT><ANAME="PRINTENVREF"></A><BCLASS="COMMAND">printenv</B></DT><DD><P>Show all the <AHREF="othertypesv.html#ENVREF">environmental	      variables</A> set for a particular user.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>printenv | grep HOME</B></TT> <TTCLASS="COMPUTEROUTPUT">HOME=/home/bozo</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="LPREF"></A><BCLASS="COMMAND">lp</B></DT><DD><P>The <BCLASS="COMMAND">lp</B> and <BCLASS="COMMAND">lpr</B>	      commands send file(s) to the print queue, to be printed as	      hard copy.	      <ANAME="AEN12974"HREF="#FTN.AEN12974">[2]</A>	      These commands trace the origin of their names to the	      line printers of another era.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>lp file1.txt</B></TT>	        or <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>lp		&#60;file1.txt</B></TT></P><P>It is often useful to pipe the formatted output from	        <BCLASS="COMMAND">pr</B> to <BCLASS="COMMAND">lp</B>.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pr -options file1.txt | lp</B></TT>	        </P><P>Formatting packages, such as <AHREF="textproc.html#GROFFREF">groff</A> and		<ICLASS="FIRSTTERM">Ghostscript</I> may send their output		directly to <BCLASS="COMMAND">lp</B>.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>groff -Tascii file.tr | lp</B></TT>	        </P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>gs -options | lp file.ps</B></TT>	        </P><P>Related commands are <BCLASS="COMMAND">lpq</B>, for viewing	        the print queue, and <BCLASS="COMMAND">lprm</B>, for removing		jobs from the print queue.</P></DD><DT><ANAME="TEEREF"></A><BCLASS="COMMAND">tee</B></DT><DD><P>[UNIX borrows an idea from the plumbing trade.]</P><P>This is a redirection operator, but with a difference. Like the	      plumber's <ICLASS="FIRSTTERM">tee,</I> it permits <SPANCLASS="QUOTE">"siponing              off"</SPAN> <SPANCLASS="emphasis"><ICLASS="EMPHASIS">to a file </I></SPAN>the output of a command 	      or commands within a pipe, but without affecting the result. This is	      useful for printing an ongoing process to a file or paper, perhaps to	      keep track of it for debugging purposes.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN">                              (redirection)                             |----&#62; to file                             |   ==========================|====================   command ---&#62; command ---&#62; |tee ---&#62; command ---&#62; ---&#62; output of pipe   =============================================== 	      </PRE></TD></TR></TABLE><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat listfile* | sort | tee check.file | uniq &#62; result.file</PRE></TD></TR></TABLE></P><P>(The file <TTCLASS="FILENAME">check.file</TT> contains	        the concatenated sorted <SPANCLASS="QUOTE">"listfiles,"</SPAN>	        before the duplicate lines are removed by <AHREF="textproc.html#UNIQREF">uniq</A>.)</P></DD><DT><ANAME="MKFIFOREF"></A><BCLASS="COMMAND">mkfifo</B></DT><DD><P><ANAME="NAMEDPIPEREF"></A>This obscure command	      creates a <ICLASS="FIRSTTERM">named pipe</I>, a temporary	      <ICLASS="FIRSTTERM">first-in-first-out buffer</I> for	      transferring data between processes.		<ANAME="AEN13038"HREF="#FTN.AEN13038">[3]</A>	      Typically, one process writes to the FIFO, and the other	      reads from it. See <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# This short script by Omair Eshkenazi.   3&nbsp;# Used in ABS Guide with permission (thanks!).   4&nbsp;   5&nbsp;mkfifo pipe1   6&nbsp;mkfifo pipe2   7&nbsp;   8&nbsp;(cut -d' ' -f1 | tr "a-z" "A-Z") &#62;pipe2 &#60;pipe1 &#38;   9&nbsp;ls -l | tr -s ' ' | cut -d' ' -f3,9- | tee pipe1 |  10&nbsp;cut -d' ' -f2 | paste - pipe2  11&nbsp;  12&nbsp;rm -f pipe1  13&nbsp;rm -f pipe2  14&nbsp;  15&nbsp;# No need to kill background processes when script terminates (why not?).  16&nbsp;  17&nbsp;exit $?  18&nbsp;  19&nbsp;Now, invoke the script and explain the output:  20&nbsp;sh mkfifo-example.sh  21&nbsp;  22&nbsp;4830.tar.gz          BOZO  23&nbsp;pipe1   BOZO  24&nbsp;pipe2   BOZO  25&nbsp;mkfifo-example.sh    BOZO  26&nbsp;Mixed.msg BOZO</PRE></TD></TR></TABLE>            </P></DD><DT><ANAME="PATHCHKREF"></A><BCLASS="COMMAND">pathchk</B></DT><DD><P>This command checks the validity of a filename. If the	      filename exceeds the maximum allowable length (255	      characters) or one or more of the directories in	      its path is not searchable, then an error message	      results.</P><P>Unfortunately, <BCLASS="COMMAND">pathchk</B> does	      not return a recognizable error code, and it is therefore	      pretty much useless in a script. Consider instead the	      <AHREF="fto.html#RTIF">file test operators</A>.</P></DD><DT><ANAME="DDREF"></A><BCLASS="COMMAND">dd</B></DT><DD><P>This is the somewhat obscure and much feared <ICLASS="FIRSTTERM">data	      duplicator</I> command. Originally a utility	      for exchanging data on magnetic tapes between UNIX	      minicomputers and IBM mainframes, this command still	      has its uses.  The <BCLASS="COMMAND">dd</B> command simply	      copies a file (or <TTCLASS="FILENAME">stdin/stdout</TT>), but	      with conversions. <ANAME="DDCONVERSIONS"></A>Possible	      conversions are ASCII/EBCDIC,	        		<ANAME="AEN13075"HREF="#FTN.AEN13075">[4]</A>	      upper/lower case, swapping of byte pairs between input	      and output, and skipping and/or truncating the head or	      tail of the input file.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Converting a file to all uppercase:   2&nbsp;   3&nbsp;dd if=$filename conv=ucase &#62; $filename.uppercase   4&nbsp;#                    lcase   # For lower case conversion</PRE></TD></TR></TABLE>            </P><P><ANAME="DDOPTIONS"></A></P><P>Some basic options to <BCLASS="COMMAND">dd</B> are:                <UL><LI><P>if=INFILE</P><P>INFILE is the <ICLASS="FIRSTTERM">source</I>		        file.</P></LI><LI><P>of=OUTFILE</P><P>OUTFILE is the <ICLASS="FIRSTTERM">target</I>		        file, the file that will have the data written to it.</P></LI><LI><P>bs=BLOCKSIZE</P><P>This is the size of each block of data being read		        and written, usually a power of 2.</P></LI><LI><P>skip=BLOCKS</P><P>How many blocks of data to skip in INFILE before		        starting to copy. This is useful when the INFILE has			<SPANCLASS="QUOTE">"garbage"</SPAN> or garbled data in its			header or when it is desirable to copy only a portion			of the INFILE.</P></LI><LI><P>seek=BLOCKS</P><P>How many blocks of data to skip in OUTFILE before		        starting to copy, leaving blank data at beginning			of OUTFILE.</P></LI><LI><P>count=BLOCKS</P><P>Copy only this many blocks of data, rather than the		        entire INFILE.</P></LI><LI><P>conv=CONVERSION</P><P>Type of conversion to be applied to INFILE data                        before copying operation.</P></LI></UL>            </P><P>A <TTCLASS="USERINPUT"><B>dd --help</B></TT> lists all the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -