old-www/LDP/GNU-Linux-Tools-Summary/html/x2361.htm

503 lines
7.1 KiB
HTML

<HTML
><HEAD
><TITLE
>Usage</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GNU/Linux Command-Line Tools Summary"
HREF="book1.htm"><LINK
REL="UP"
TITLE="Directing Input/Output"
HREF="c2269.htm"><LINK
REL="PREVIOUS"
TITLE="Directing Input/Output"
HREF="c2269.htm"><LINK
REL="NEXT"
TITLE="Command Substitution"
HREF="x2563.htm"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c2269.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 6. Directing Input/Output</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x2563.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="USAGE-INPUT-OUTPUT"
></A
>Usage</H1
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>&gt;</DT
><DD
><P
><A
NAME="AEN2368"
></A
>The greater <A
NAME="AEN2370"
></A
>than symbol <A
NAME="AEN2372"
></A
>is used to send <A
NAME="AEN2374"
></A
>information somewhere (for example a text file)</P
><P
>Example:</P
><PRE
CLASS="SCREEN"
>cat file1 file2 &#62; file1_and_2.txt</PRE
><P
>This will concatenate <A
NAME="AEN2379"
></A
>the files together into one big file named &ldquo;file1_and_2.txt&rdquo;. Note that this will overwrite <A
NAME="AEN2381"
></A
>any existing <A
NAME="AEN2383"
></A
>file.</P
></DD
><DT
>&lt;</DT
><DD
><P
><A
NAME="AEN2389"
></A
>The less <A
NAME="AEN2391"
></A
>than symbol will insert <A
NAME="AEN2393"
></A
>information from somewhere (a text file) as if you typed it yourself. Often used with commands that are designed <A
NAME="AEN2395"
></A
>to get information from standard input <A
NAME="AEN2397"
></A
>only.</P
><P
>For example (using tr):</P
><PRE
CLASS="SCREEN"
>tr '[A-Z]' '[a-z]' &#60; fileName.txt &#62; fileNameNew.txt</PRE
><P
>The example above would insert <A
NAME="AEN2402"
></A
>the contents <A
NAME="AEN2404"
></A
>of &ldquo;fileName.txt&rdquo; into the input <A
NAME="AEN2406"
></A
>of <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>tr</I
></SPAN
> <A
NAME="AEN2409"
></A
>and output the results <A
NAME="AEN2411"
></A
>to &ldquo;fileNameNew.txt&rdquo;.</P
></DD
><DT
>&gt;&gt;</DT
><DD
><P
><A
NAME="AEN2417"
></A
>The &gt;&gt; symbol <A
NAME="AEN2419"
></A
>appends (adds) information to the end of a file or creates one if the file doesn't exist.<A
NAME="AEN2421"
></A
></P
></DD
><DT
>&lt;&lt;</DT
><DD
><P
><A
NAME="AEN2427"
></A
>The &lt;&lt; symbol <A
NAME="AEN2429"
></A
>is sometimes used with commands that use standard input<A
NAME="AEN2431"
></A
> to take information. You simply type <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>&lt;&lt; word</I
></SPAN
> (where word can be any string) at the end <A
NAME="AEN2434"
></A
>of the command. However its main use is in shell scripting.<A
NAME="AEN2436"
></A
> </P
><P
>The command takes your input until you type &ldquo;word&rdquo;, which causes the command to terminate <A
NAME="AEN2439"
></A
>and process <A
NAME="AEN2441"
></A
>the input. </P
><P
>Using &lt;&lt; is similar to using
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
<A
NAME="AEN2447"
></A
>
(EOF key)<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>, </I
></SPAN
>except it uses a string <A
NAME="AEN2450"
></A
>to perform <A
NAME="AEN2452"
></A
>the end-of-file function. This design allows it to be used in shell scripts.<A
NAME="AEN2454"
></A
></P
><P
>For example type &quot;cat&quot;<A
NAME="AEN2457"
></A
> (with no options...) and it will work on standard input.<A
NAME="AEN2459"
></A
></P
><P
>To stop <A
NAME="AEN2462"
></A
>entering standard input <A
NAME="AEN2464"
></A
>you would normally hit
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
<A
NAME="AEN2469"
></A
>
.</P
><P
>As an alternative <A
NAME="AEN2472"
></A
>you can type &quot;cat &lt;&lt; FINISHED&quot;, then type what you want. </P
><P
>When you are finished,<A
NAME="AEN2475"
></A
> instead of hitting <A
NAME="AEN2477"
></A
>
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
<A
NAME="AEN2482"
></A
>
you could type &quot;FINISHED&quot;<A
NAME="AEN2484"
></A
> and it will end (the word FINISHED will not be recorded).</P
><P
></P
></DD
><DT
>2&gt;</DT
><DD
><P
><A
NAME="AEN2491"
></A
>Redirects error output.<A
NAME="AEN2493"
></A
> For example, to redirect <A
NAME="AEN2495"
></A
>the error output <A
NAME="AEN2497"
></A
>to /dev/null<A
NAME="AEN2499"
></A
>, so you do not see it, simply append this to the end of another command...</P
><P
>For example:</P
><PRE
CLASS="SCREEN"
>make some_file 2&#62; /dev/null</PRE
><P
>This will run make on a file and send <A
NAME="AEN2504"
></A
>all error output to /dev/null</P
></DD
><DT
>|</DT
><DD
><P
><A
NAME="AEN2510"
></A
><A
NAME="AEN2512"
></A
>The &ldquo;pipe&rdquo; command allows the output of one command to be sent to the input of another.</P
><P
>For example:</P
><PRE
CLASS="SCREEN"
>cat file1.txt file2.txt | less</PRE
><P
>Concatenates <A
NAME="AEN2517"
></A
>the files together, then runs <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>less</I
></SPAN
> on them. If you are only going to look <A
NAME="AEN2520"
></A
>at a single file, you would simply use <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>less<A
NAME="AEN2523"
></A
></I
></SPAN
> on the file...</P
></DD
><DT
>tee</DT
><DD
><P
><A
NAME="AEN2529"
></A
>Sends output of a program to a file and to standard output.<A
NAME="AEN2531"
></A
> Think of it as a T intersection...it goes two ways.</P
><P
>For example:</P
><PRE
CLASS="SCREEN"
>ls /home/user | tee my_directories.txt</PRE
><P
>Lists <A
NAME="AEN2536"
></A
>the files (displays the output on the screen) and sends <A
NAME="AEN2538"
></A
>the output to a file: &ldquo;my_directories.txt&rdquo;.</P
></DD
><DT
>&amp;&gt;</DT
><DD
><P
><A
NAME="AEN2544"
></A
>Redirects standard output <A
NAME="AEN2546"
></A
>and error output <A
NAME="AEN2548"
></A
>to a specific <A
NAME="AEN2550"
></A
>location.<A
NAME="AEN2552"
></A
></P
><P
>For example:</P
><PRE
CLASS="SCREEN"
>make &#38;&#62; /dev/null</PRE
><P
>Sends <A
NAME="AEN2557"
></A
>both error output <A
NAME="AEN2559"
></A
>and standard output <A
NAME="AEN2561"
></A
>to /dev/null so you won't see anything...</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c2269.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x2563.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Directing Input/Output</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c2269.htm"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Command Substitution</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>