<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://freebsdwiki.net/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ninereasons</id>
		<title>FreeBSDwiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ninereasons"/>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Special:Contributions/Ninereasons"/>
		<updated>2026-04-05T16:35:52Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.18.0</generator>

	<entry>
		<id>http://freebsdwiki.net/index.php/Gvinum</id>
		<title>Gvinum</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Gvinum"/>
				<updated>2007-08-15T21:31:11Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: an LVM&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(NOTE: for practical tasks, you should probably see [[gmirror]] or [[graid3]] instead of this article.)&lt;br /&gt;
&lt;br /&gt;
'''{{PAGENAME}}''' is a Logical Volume Manager, providing kernel-level software [[RAID]] for the FreeBSD (and NetBSD) operating systems.  ''Gvinum'' is the replacement, since FreeBSD 5.3, for the [http://www.vinumvm.org/ ''Vinum'' Volume Manager], re-written to utilize [[geom]] communication methods, and is included in the FreeBSD core distribution. Many of the ''vinum'' functions are reproduced in ''gvinum'', but not all.&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* The fourth chapter, concerning Vinum, from [http://www.oreilly.com/catalog/cfreebsd/index.html The Complete FreeBSD] is available on-line as  [http://www.vinumvm.org/cfbsd/vinum.pdf PDF] or [http://www.vinumvm.org/cfbsd/vinum.txt text]&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:System Commands]]&lt;br /&gt;
[[Category:RAID]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/User_talk:Ninereasons</id>
		<title>User talk:Ninereasons</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/User_talk:Ninereasons"/>
				<updated>2007-08-15T21:26:57Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* btw, thought you'd be interested to know */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Sure, I'll take it... ==&lt;br /&gt;
&lt;br /&gt;
Your documentation is welcome, as long as you provide some background info on what you're documenting... perhaps I should blush, but I'm not even sure what an LSI device is, exactly. =)  Glad you've found the wiki useful!  --[[User:Jimbo|Jimbo]] 08:11, 6 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Goodness, no reason to blush.  I hope this material isn't too obscure.  It seemed to me that since your site has search engine presence, the documentation might be found and improved more readily if it were parked here.  Thank you for accomodating me - for the good of the community and all that! [[User:Ninereasons|Ninereasons]] 14:26, 6 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
: I wanted to assure you in passing that, I have no connection whatever with LSI Logic or megarc, other than as a user who found FreeBSD support of these cards a bit scanty.  I was frustrated by the fact that it took me a good part of two days to find out what the alarm codes meant, and how to bring a replacement drive online without rebooting.  In fact, I thought at first that the alarm was coming from the motherboard, because the drive would not rebuild, and the machine would not boot while the volume was degraded.  I was within a hair's breadth of trying to replace the board! So, I'm the one who should blush. [[User:Ninereasons|Ninereasons]] 13:33, 7 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
Wouldn't bother me a bit if you WERE connected to LSI Logic or megarc, as long as your documentation was useful, pertinent, and not full of &amp;quot;omg you gots to have teh megarc it is teh K-RAD!!!1&amp;quot; :) --[[User:Jimbo|Jimbo]] 13:44, 7 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== so....what is an LSI? ==&lt;br /&gt;
&lt;br /&gt;
i don't deal with storage, so i've no idea...&lt;br /&gt;
&lt;br /&gt;
--[[User:Dave|Dave]] 22:51, 6 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
: &amp;quot;LSI Logic&amp;quot; is a manufacturer/marketer of consumer product controller chips and storage devices. [[User:Ninereasons|Ninereasons]] 13:10, 7 April 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== machine names and style consistency ==&lt;br /&gt;
&lt;br /&gt;
As long as the machine name stays consistent within a single article, there is no one single machine name we particularly want in every single article... at least, not at this time.  If we were going to standardize every single article to a single machine name, we'd probably want to pick something fictitious anyway.&lt;br /&gt;
&lt;br /&gt;
If you come across an article that refers to ph34r in one block and samizdata or oyabun (or whatever) later on - and it's ''not'' supposed to be an article about two different machines in the first place - please jump right on in and sanitize it.  But otherwise leave 'em like it is please. =)  --[[User:Jimbo|Jimbo]] 20:36, 16 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Alrighty. [[User:Ninereasons|Ninereasons]] 11:17, 17 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== btw, thought you'd be interested to know ==&lt;br /&gt;
&lt;br /&gt;
I'm getting about 20 or so search engine hits per month on &amp;quot;megarc&amp;quot;, so it would seem your articles are serving the community well. =) --[[User:Jimbo|Jimbo]] 10:25, 25 May 2007 (EDT)&lt;br /&gt;
: I'm glad to hear that, Jimbo.  Thanks for letting me know. [[User:Ninereasons|Ninereasons]] 17:26, 15 August 2007 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Tomcat</id>
		<title>Tomcat</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Tomcat"/>
				<updated>2006-10-21T20:44:08Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Setup Environment Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Tomcat is used as a server to run Java Servlets.  It can be used stand alone or through Apache.  Java Servlets is a great language which is very flexible.  You can perform balance loading and connect to many great database engines such as postgres.  There are many frameworks which you can use to speed up development and keep your code organised, a popular framework is 'struts'.&lt;br /&gt;
&lt;br /&gt;
In this document I will explain how to setup jakarta-tomcat-5.5&lt;br /&gt;
&lt;br /&gt;
==Installation==&lt;br /&gt;
===Versions===&lt;br /&gt;
The packages and version numbers I will be installing/working with here are:&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.freebsdwiki.net/index.php/Installing_FreeBSD_-_Standard_Installation FreeBSD 6.1]&lt;br /&gt;
&amp;lt;li&amp;gt;jakarta-tomcat-5.5.17_1&lt;br /&gt;
&amp;lt;li&amp;gt;jakarta-struts 1.2.4&lt;br /&gt;
&amp;lt;li&amp;gt;apache-ant-1.6.5_1&lt;br /&gt;
&amp;lt;li&amp;gt;diablo-jdk-freebsd6.i386.1.5.0.07.00&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.freebsdwiki.net/index.php/PostgreSQL PostgreSQL 8.1.4]&lt;br /&gt;
&amp;lt;li&amp;gt;KATE 2.5.3&lt;br /&gt;
&lt;br /&gt;
===Install Java===&lt;br /&gt;
cd /usr/ports/java/diablo-jdk15/ &amp;amp;&amp;amp; make install clean&lt;br /&gt;
Java will require you to download a heap of files separately through the browser and place them in /usr/ports/distfiles&lt;br /&gt;
&lt;br /&gt;
===Install Tomcat===&lt;br /&gt;
Install tomcat from ports as the post-install scripts help configure the system faster.&lt;br /&gt;
# cd /usr/ports/*/tomcat55&lt;br /&gt;
# make install&lt;br /&gt;
&lt;br /&gt;
===Install ANT===&lt;br /&gt;
Used for compiling&lt;br /&gt;
# pkg_add -r apache-ant&lt;br /&gt;
&lt;br /&gt;
==Configure==&lt;br /&gt;
There is a lot of configuration and tests which you can do in tomcat.  Here I'm going to show a basic setup in which you should be able to start some projects.&lt;br /&gt;
&lt;br /&gt;
===Startup===&lt;br /&gt;
If you want all the servers to load up on boot here is what you have to add to /etc/rc.conf&lt;br /&gt;
 tomcat55_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
 postgresql_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Setup Environment Variables===&lt;br /&gt;
Editing /etc/profile&lt;br /&gt;
 JAVA_HOME=/usr/local/diablo-jdk1.5.0; export JAVA_HOME&lt;br /&gt;
 CATALINA_HOME=&amp;quot;/usr/local/tomcat5.5&amp;quot;; export CATALINA_HOME&lt;br /&gt;
 CATALINA_BASE=&amp;quot;/usr/local/tomcat5.5&amp;quot;; export CATALINA_BASE&lt;br /&gt;
 CATALINA_TMPDIR=&amp;quot;/usr/local/tomcat5.5/temp&amp;quot;; export CATALINA_TMPDIR&lt;br /&gt;
 &lt;br /&gt;
 CLASSPATH=&amp;quot;/sbin:/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/local/tomcat5.5/common/lib/servlet-api.jar:&lt;br /&gt;
 /usr/local/tomcat5.5/common/lib/jsp-api.jar:.&amp;quot;;export CLASSPATH&lt;br /&gt;
&lt;br /&gt;
===Tomcat Servlet Reloading===&lt;br /&gt;
So that your modifications are automatically reloaded on your development box edit:&lt;br /&gt;
/usr/local/tomcat5.5/conf/context.xml and change:&lt;br /&gt;
 &amp;lt;Context&amp;gt;&lt;br /&gt;
to &lt;br /&gt;
 &amp;lt;Context reloadable=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===First time startup===&lt;br /&gt;
To start the tomcat server for the first time here is what you do:&lt;br /&gt;
 shell# cd /usr/local/tomcat5.5/bin&lt;br /&gt;
 shell# bash&lt;br /&gt;
 shell# JAVA_HOME=”/usr/local/diablo-jdk1.5.0”;export JAVA_HOME&lt;br /&gt;
 shell# ./startup.sh&lt;br /&gt;
&lt;br /&gt;
==Using Tomcat==&lt;br /&gt;
The default for tomcat is http://&amp;lt;ip&amp;gt;:8180 (used to be on port 8080)&lt;br /&gt;
&lt;br /&gt;
The work area is /usr/local/tomcat5.5/webapps&lt;br /&gt;
&lt;br /&gt;
You can enter project_name.war files into the webapps directory then access them like so:&lt;br /&gt;
 http://&amp;lt;ip&amp;gt;:8180/project_name&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Ports and Packages]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Megarc</id>
		<title>Megarc</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Megarc"/>
				<updated>2006-07-24T17:41:11Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Ports and Packages]] [[Category:RAID]]&lt;br /&gt;
'''''{{PAGENAME}}''''' is a commandline utility that provides an interface to many of the configuration and reporting functions for LSI Logic's MegaRAID BIOS (http://www.lsilogic.com).   &lt;br /&gt;
&lt;br /&gt;
==Megarc port for FreeBSD==&lt;br /&gt;
The ''megarc'' utility ships as a binary, without any accompanying documentation, on the &amp;quot;Megaraid Universal Software Suite&amp;quot; CD which accompanies any boxed LSI Logic RAID storage adapter.  It is also found in the FreeBSD ports collection.&lt;br /&gt;
&lt;br /&gt;
* On the CD that comes with the storage adapter, the utility is found in two places.  It is part of a zipped bundle in &amp;lt;cd&amp;gt;:\SW_Components\Drivers\dr_freebsd_1.51.zip on our distribution.  The zip file contents are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  Length     Date   Time    Name&lt;br /&gt;
 --------    ----   ----    ----&lt;br /&gt;
   130448  04-18-05 14:10   MegaRC 1.04.zip&lt;br /&gt;
    12953  10-08-04 16:05   amr_x86_64_ver1_51FreeBSD5.3.tgz&lt;br /&gt;
   153600  03-17-05 15:19   code.tar&lt;br /&gt;
      535  02-10-05 11:02   FreeBSDDriverUpdate.txt&lt;br /&gt;
    12774  10-12-04 19:38   amr_i386_ver_1_51_FreeBSD5.3.tgz&lt;br /&gt;
 --------                   -------&lt;br /&gt;
   310310                   5 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The utility is also found on the CD by itself, at &amp;quot;&amp;lt;cd&amp;gt;:\SW_Components\Utilities\ut_FreeBSD_MegaRC 1.04.zip&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
* It can be also be installed from the [http://www.freebsd.org/cgi/cvsweb.cgi/ports/sysutils/megarc/ ports collection] at /usr/ports/sysutils/megarc.  The port extracts the binary from the bundled dr_freebsd_*.zip  Our example was installed from the ports collection on 6.1-PRERELEASE FreeBSD  amd64&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# file /usr/local/sbin/megarc&lt;br /&gt;
 /usr/local/sbin/megarc: ELF 32-bit LSB executable, Intel 80386,&lt;br /&gt;
 version 1 (FreeBSD), for FreeBSD 5.2.1, statically linked, stripped&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Documentation==&lt;br /&gt;
The documentation for megarc is limited to the output of its ?/help functions.  Since these [[RAID]] adapters are reasonably popular, and the commands are a bit scary and somewhat obscurely named, this page might prove helpful to someone even though it's not complete.  &lt;br /&gt;
&lt;br /&gt;
Megarc must be run with sufficient privileges, or the adapter will not be found.  &amp;quot;?&amp;quot; and &amp;quot;help&amp;quot; are not synonymous.  ''?'' (mis-named &amp;quot;complete help&amp;quot;) gives brief syntax hints.  ''help'' gives a fuller description of what the command does, and its arguments and usage.  &lt;br /&gt;
&lt;br /&gt;
* To see the commands available, as root type:&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;megarc ?&amp;lt;/code&amp;gt;&lt;br /&gt;
    First Parameter should be :&lt;br /&gt;
       -AllAdpInfo &lt;br /&gt;
       ... etc ...&lt;br /&gt;
*To see a list of commands and syntax, type:&amp;lt;br /&amp;gt; &amp;lt;code&amp;gt;megarc help&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 -------------------------------------------------------------------------&lt;br /&gt;
        * Convention Used:&lt;br /&gt;
           [Cmd = The name of the utility]&lt;br /&gt;
           opt1|opt2|opt3 =&amp;gt; Only one of these can be specified&lt;br /&gt;
&lt;br /&gt;
 -------------------------------------------------------------------------&lt;br /&gt;
 Cmd ?: Gives complete help&lt;br /&gt;
 Cmd -functionality ?: functionality specific help&lt;br /&gt;
 Example:&lt;br /&gt;
 Cmd -ctlrInfo ? :help on -ctlrinfo option&lt;br /&gt;
&lt;br /&gt;
 -------------------------------------------------------------------------&lt;br /&gt;
 Usage: Cmd -ctlrInfo  -aX &lt;br /&gt;
 ... etc ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* '''Example:'''&amp;lt;br /&amp;gt; &amp;lt;code&amp;gt; megarc -ctlrInfo help &amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  **********************************************************************&lt;br /&gt;
&lt;br /&gt;
  usage :&lt;br /&gt;
  cmd -ctlrInfo -aX&lt;br /&gt;
  : Shows general adapter info of adapter&lt;br /&gt;
  **********************************************************************&lt;br /&gt;
  where :&lt;br /&gt;
    cmd : name of the utility&lt;br /&gt;
    -aX : adapter number X(max 12 adapters). X=0..11&lt;br /&gt;
 **********************************************************************&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
For the examples below, we are using an LSILogic 150-6 x64 SATA RAID adapter on a 32-bit PCI slot - &amp;quot;adapter 0&amp;quot; on the system - with a RAID 5 volume consisting of five (5) physical SATA drives of 400GB each.  The hot-spare belongs at target 00 but is being replaced at the time of this writing.&lt;br /&gt;
&lt;br /&gt;
Below are some brief descriptions of some of the commands, and some examples of output from a few of the information functions.  These command-options are not case-sensitive.&lt;br /&gt;
&lt;br /&gt;
===megarc -dispCfg -a0 ===&lt;br /&gt;
Display the configuration for adapter ''0''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
          Logical Drive : 0( Adapter: 0 ):  Status: OPTIMAL&lt;br /&gt;
        ---------------------------------------------------&lt;br /&gt;
        SpanDepth :01     RaidLevel: 5  RdAhead : No  Cache: DirectIo&lt;br /&gt;
        StripSz   :064KB   Stripes  : 5  WrPolicy: WriteThru&lt;br /&gt;
&lt;br /&gt;
        Logical Drive 0 : SpanLevel_0 Disks&lt;br /&gt;
        Chnl  Target  StartBlock   Blocks      Physical Target Status&lt;br /&gt;
        ----  ------  ----------   ------      ----------------------&lt;br /&gt;
        0      01    0x00000000   0x2e936800   ONLINE&lt;br /&gt;
        0      02    0x00000000   0x2e936800   ONLINE&lt;br /&gt;
        0      03    0x00000000   0x2e936800   ONLINE&lt;br /&gt;
        0      04    0x00000000   0x2e936800   ONLINE&lt;br /&gt;
        0      05    0x00000000   0x2e936800   ONLINE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -LogPhysInfo -a0 ===&lt;br /&gt;
Display the physical drive information for each of the logical drives on adapter ''0''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        Logical drive 0: RaidLevel 5&lt;br /&gt;
&lt;br /&gt;
        Physical Drive Information&lt;br /&gt;
        Channel 0&lt;br /&gt;
                381549MB drive ID 1&lt;br /&gt;
        CoerSZ: 781412352(Sectors) 381549(MB)  RawSZ: 781422255(Sectors)&lt;br /&gt;
                381549MB drive ID 2&lt;br /&gt;
        CoerSZ: 781412352(Sectors) 381549(MB)  RawSZ: 781422255(Sectors)&lt;br /&gt;
                381549MB drive ID 3&lt;br /&gt;
        CoerSZ: 781412352(Sectors) 381549(MB)  RawSZ: 781422255(Sectors)&lt;br /&gt;
                381549MB drive ID 4&lt;br /&gt;
        CoerSZ: 781412352(Sectors) 381549(MB)  RawSZ: 781422255(Sectors)&lt;br /&gt;
                381549MB drive ID 5&lt;br /&gt;
        CoerSZ: 781412352(Sectors) 381549(MB)  RawSZ: 781422255(Sectors)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -ScfgAndParm|-DfcfgAndParm|-RcfgAndParm -fFileName -a0 ===&lt;br /&gt;
Save, Display, or Restore the configuration and parameters for adapter ''0'', in ''FileName''.  ''FileName'' stores the same output provided by -dispCfg in a binary format, making it possible to directly load the stored configuration from the file.&lt;br /&gt;
&lt;br /&gt;
===megarc -physOn pd[c0:t0,c1:t1....] -a0===&lt;br /&gt;
&lt;br /&gt;
Set the state of the designated drive(s) to ''Online''.  ''pd[c:t]'' refers to at least one physical drive by channel and target.  ''-aN'' here as elsewere is the adapter number [required]&lt;br /&gt;
&lt;br /&gt;
If the physical drive does not exist or if it isn't in failed state, the utility exits with no harm done.&lt;br /&gt;
&lt;br /&gt;
An example of this command under our present configuration would be:  &lt;br /&gt;
 megarc -physOn -a0 pd[0:1]&lt;br /&gt;
&lt;br /&gt;
===megarc -phys -chAll -idAll -a0===&lt;br /&gt;
Show the physical drive description for each device on all channels managed by adapter ''0''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        Adapter 0, Channel 0, Target ID 1     &lt;br /&gt;
        Type: DISK                 Vendor      : WDC&lt;br /&gt;
        Product: WD4000KD-00NAB0   Revision    : 01.0&lt;br /&gt;
        Synchronous   : No         Wide-32     : No    Wide-16: No&lt;br /&gt;
        LinkCmdSupport: No         TagQ support: No    RelAddr: No&lt;br /&gt;
        Removable     : No         SoftReset   : No    AENC   : No&lt;br /&gt;
   etc...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -physdrvSerialInfo -chAll -idAll -a0===&lt;br /&gt;
Show the serial number for each physical drive on each channel for all serial devices managed by adapter ''0'' (This doesn't look correct or helpful).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  Adapter 0, Channel 0, Target ID 1 &lt;br /&gt;
&lt;br /&gt;
  PhysDrvSerial#:      WD-W&lt;br /&gt;
&lt;br /&gt;
 etc ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -pdFailInfo -chAll -idAll -a0===&lt;br /&gt;
Show the failure history for each device on all channels managed by adapter ''0''.  &lt;br /&gt;
&lt;br /&gt;
===megarc -setRbldRate|-getRbldRate -a0 ===&lt;br /&gt;
Get the rebuild rate for adapter ''0''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# megarc -getRbldRate -a0&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
 **********************************************************************&lt;br /&gt;
 RebuildRate of Adapter-0 is 30&lt;br /&gt;
 **********************************************************************&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -ctlrInfo -a0===&lt;br /&gt;
Display information about adapter ''0''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  **********************************************************************&lt;br /&gt;
  Information of Adapter-0 (#Adapter(s) on system: 1)&lt;br /&gt;
  **********************************************************************&lt;br /&gt;
&lt;br /&gt;
  Firmware Version : 713N         BIOS Version : G119&lt;br /&gt;
  Logical Drives : 01             DRAM : 64MB&lt;br /&gt;
  Rebuild Rate : 30%&lt;br /&gt;
  Flush Interval : 4 secs&lt;br /&gt;
  Number Of Chnls : 1             Bios Status : Enabled&lt;br /&gt;
  Alarm State : Enabled           Auto Rebuild : Enabled&lt;br /&gt;
  FW : SPAN-8, 40-LD              BIOS Config AutoSelection : USER&lt;br /&gt;
  BIOS Echos Mesg : ON            BIOS Stops On Error : ON&lt;br /&gt;
  Initiator Id : 16(Clustered Firmware)&lt;br /&gt;
  Board SN: -17179869&lt;br /&gt;
  **********************************************************************&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===megarc -getXFerRate|-setXFerRate -a0 -chAll===&lt;br /&gt;
Get or set the transfer rate for all channels on adapter ''0''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# megarc -getXFerRate -a0 -ch0&lt;br /&gt;
&lt;br /&gt;
   **********************************************************************&lt;br /&gt;
   Transfer Rate of Adapter-0 Channel-0 is 160M&lt;br /&gt;
   **********************************************************************&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-07-12T21:45:56Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Shell pipelines and redirection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Shell redirection operators include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (0-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
=== ''tcsh'' and ''sh'' ===&lt;br /&gt;
# Write output to a file&lt;br /&gt;
#: % mycmd &amp;gt; out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to a file&lt;br /&gt;
#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ''tcsh'' only ===&lt;br /&gt;
# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to out.txt; discard messages&lt;br /&gt;
#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
=== ''sh'' only ===&lt;br /&gt;
# Discard errors, watch output &lt;br /&gt;
#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to out.txt; discard messages&lt;br /&gt;
#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Assign a variable from stored.txt&lt;br /&gt;
#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Use all unique lines in stored.txt as an argument variable for ''mycmd'', appending to result.txt&lt;br /&gt;
#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
:: ''-sh: fails without messages''&lt;br /&gt;
:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;/dev/stderr | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;/dev/stdout | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-05T05:04:26Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; &amp;quot;creates/updates the Index&amp;quot; by a system call to &amp;lt;code&amp;gt;make index&amp;lt;/code&amp;gt;, so it's just as slow.   There's a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that &amp;lt;u&amp;gt;incrementally&amp;lt;/u&amp;gt; keeps the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''portsdb -U'''u''''' before running portupgrade (''portsdb -U'' would seem sufficient).  On the other hand, this is the procedure recommended on the [http://www.freebsd.org/cgi/man.cgi?query=portsdb&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.1-RELEASE+and+Ports&amp;amp;format=html portsdb man page], and the ''-u'' doesn't cost a lot of time. [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
:: Hello Ninereasons, I put this in the discussion section; to see if there could be a consensus of users; aka a peer review of bsd users.  I am somewhat hesitant to make any official changes to a wiki until I get some feedback as well as proof reading what I have written (IE: the &amp;quot;make fetchindex&amp;quot; typing error).  As for the extra tools; with respect to managing / updating the index; I would like to see this as bare metal as possible.  In other words; the tools exist to update the index; no need to add more tools (imho).  I am trying to look at it from a new users perspective.  I could very well be wrong or misguided in that view.  PS: never used a wiki before; I don't wish to break the original documentation. PSS: thanks for the feedback.&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 21:49, 4 July 2006 (EDT)Pete&lt;br /&gt;
&lt;br /&gt;
::: Hi Pete.  You can call me &amp;quot;Mark&amp;quot;.  The beauty of a wiki is that you won't &amp;quot;break the original&amp;quot; by making a useful contribution like the one you're contemplating.  If you make mistakes, we'll fix them.  If you seem to be wrong, which isn't the case here, we would just restore what you messed up - and no hard feelings. I'm a guest here, just like you.  But even as a guest, I can invite you to log in with a username and give it a try.  [[User:Ninereasons|Ninereasons]] 01:03, 5 July 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== hey Pete ==&lt;br /&gt;
&lt;br /&gt;
How about registering an account and using it, if you plan on being a recurring contributor?&lt;br /&gt;
&lt;br /&gt;
It's not ''required'' by any means, but it makes it a lot easier for the rest of us to keep track of who's who (and whether or not a new edit is likely to be spam or vandalism).&lt;br /&gt;
&lt;br /&gt;
Thanks, and welcome to the wiki!  --[[User:Jimbo|Jimbo]] 23:01, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-05T05:03:35Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; &amp;quot;creates/updates the Index&amp;quot; by a system call to &amp;lt;code&amp;gt;make index&amp;lt;/code&amp;gt;, so it's just as slow.   There's a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that &amp;lt;u&amp;gt;incrementally&amp;lt;/u&amp;gt; keeps the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''portsdb -U'''u''''' before running portupgrade (''portsdb -U'' would seem sufficient).  On the other hand, this is the procedure recommended on the [http://www.freebsd.org/cgi/man.cgi?query=portsdb&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.1-RELEASE+and+Ports&amp;amp;format=html portsdb man page], and the ''-u'' doesn't cost a lot of time. [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
:: Hello Ninereasons, I put this in the discussion section; to see if there could be a consensus of users; aka a peer review of bsd users.  I am somewhat hesitant to make any official changes to a wiki until I get some feedback as well as proof reading what I have written (IE: the &amp;quot;make fetchindex&amp;quot; typing error).  As for the extra tools; with respect to managing / updating the index; I would like to see this as bare metal as possible.  In other words; the tools exist to update the index; no need to add more tools (imho).  I am trying to look at it from a new users perspective.  I could very well be wrong or misguided in that view.  PS: never used a wiki before; I don't wish to break the original documentation. PSS: thanks for the feedback.&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 21:49, 4 July 2006 (EDT)Pete&lt;br /&gt;
&lt;br /&gt;
::: Hi Pete.  You can call me &amp;quot;Mark&amp;quot;.  The beauty of a wiki is that you won't &amp;quot;break the original&amp;quot; by making a useful contribution like the one you're contemplating.  If you make mistakes, we'll fix them.  If you seem to be wrong, which isn't the case here, we would just restore what you messed up - and no hard feeligns. I'm a guest here, just like you.  But even as a guest, I can invite you to log in and give it a try.  [[User:Ninereasons|Ninereasons]] 01:03, 5 July 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== hey Pete ==&lt;br /&gt;
&lt;br /&gt;
How about registering an account and using it, if you plan on being a recurring contributor?&lt;br /&gt;
&lt;br /&gt;
It's not ''required'' by any means, but it makes it a lot easier for the rest of us to keep track of who's who (and whether or not a new edit is likely to be spam or vandalism).&lt;br /&gt;
&lt;br /&gt;
Thanks, and welcome to the wiki!  --[[User:Jimbo|Jimbo]] 23:01, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T19:29:24Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; &amp;quot;creates/updates the Index&amp;quot; by a system call to &amp;lt;code&amp;gt;make index&amp;lt;/code&amp;gt;, so it's just as slow.   There's a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that &amp;lt;u&amp;gt;incrementally&amp;lt;/u&amp;gt; keeps the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''portsdb -U'''u''''' before running portupgrade (''portsdb -U'' would seem sufficient).  On the other hand, this is the procedure recommended on the [http://www.freebsd.org/cgi/man.cgi?query=portsdb&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.1-RELEASE+and+Ports&amp;amp;format=html portsdb man page], and the ''-u'' doesn't cost a lot of time. [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T19:18:58Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; &amp;quot;creates/updates the Index&amp;quot; by a system call to &amp;lt;code&amp;gt;make index&amp;lt;/code&amp;gt;, so it's just as slow.   There's a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that &amp;lt;u&amp;gt;incrementally&amp;lt;/u&amp;gt; keeps the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''portsdb -Uu'' before running portupgrade.  On the other hand, this is the procedure recommended on the [http://www.freebsd.org/cgi/man.cgi?query=portsdb&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.1-RELEASE+and+Ports&amp;amp;format=html portsdb man page]. [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T19:01:15Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; creates the Index or &amp;quot;updates&amp;quot; it, but as far as I can tell it doesn't do exactly what ''make index'' does - the difference might be that the ''portsdb'' script is incrementally updating the Index (I'm not fluent in ruby - and it's a complex script); but, it isn't much faster than ''make index'', so I'm not sure except that the result is an up-to-the-minute Index, like ''make index''.   There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that incrementally keep the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''portsdb -Uu'' before running portupgrade.  On the other hand, this is the procedure recommended on the [http://www.freebsd.org/cgi/man.cgi?query=portsdb&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.1-RELEASE+and+Ports&amp;amp;format=html portsdb man page]. [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:49:43Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: sigh&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; fetches the Index and &amp;quot;updates&amp;quot; it, but as far as I can tell it doesn't do exactly what ''make index'' does - the difference might be that the ''portsdb'' script is incrementally updating the Index (I'm not fluent in ruby - and it's a complex script); but, it isn't much faster than ''make index'', so I'm not sure except that the result is an up-to-the-minute Index, like ''make index''.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that incrementally keep the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''pkgdb -Uu'' before running portupgrade.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:47:37Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;pkgdb -U&amp;lt;/code&amp;gt; fetches the Index and &amp;quot;updates&amp;quot; it, but as far as I can tell it doesn't do exactly what ''make index'' does - the difference might be that the ''pkgdb'' script is incrementally updating the Index (I'm not fluent in ruby - and it's a complex script); but, it isn't much faster than ''make index'', so I'm not sure except that the result is an up-to-the-minute Index, like ''make index''.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that incrementally keep the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;pkgdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''pkgdb -Uu'' before running portupgrade.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:47:11Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;pkgdb -U&amp;lt;/code&amp;gt; fetches the Index and &amp;quot;updates&amp;quot; it, but as far as I can tell it doesn't do exactly what ''make index'' does - the difference might be that the ''portsdb'' script is incrementally updating the Index (I'm not fluent in ruby - and it's a complex script); but, it isn't much faster than ''make index'', so I'm not sure except that the result is an up-to-the-minute Index, like ''make index''.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that incrementally keep the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;pkgdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''pkgdb -Uu'' before running portupgrade.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:45:42Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' builds an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; fetches the Index and &amp;quot;updates&amp;quot; it, but as far as I can tell it doesn't do exactly what ''make index'' does - the difference might be that the ''portsdb'' script is incrementally updating the Index (I'm not fluent in ruby - and it's a complex script); but, it isn't much faster than ''make index'', so I'm not sure except that the result is an up-to-the-minute Index, like ''make index''.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that incrementally keep the Index updated, by using a cache of previous output. At least in theory, this should be faster; but I haven't used the port. &lt;br /&gt;
&lt;br /&gt;
:Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build - so, I can't see the advantage of the extra precaution of running ''pkgdb -Uu'' before running portupgrade.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:16:43Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Running ''make index'' is the only way to build an up-to-the-minute Index.  You might also mention that, ''make index'' is very, very slow.  &amp;lt;code&amp;gt;portsdb -U&amp;lt;/code&amp;gt; fetches the Index, rather than building it.  Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that make it possible to update the Index incrementally.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T18:09:11Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build.  You might also mention that, ''make index'' is very, very slow.  There's also a port in sysutils/p5-FreeBSD-Portindex, which is a set of perl scripts that make it possible to update the Index incrementally.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T17:59:13Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: Thanks for the notes.  It would be helpful to add them in to the article.  The target is ''make fetchindex'' (rather than ''makefetch index'').  Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build.  You might also mention that, ''make index'' is very, very slow.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:Portupgrade</id>
		<title>Talk:Portupgrade</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:Portupgrade"/>
				<updated>2006-07-04T17:53:52Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Putting the pieces together for portupgrade */  - the target is 'fetchindex'&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;note on the rollback: I strongly prefer showing a pipe to grep rather than the -l argument because they both accomplish the same thing, but piping to grep is a universal tool than can be used with any command or process.  It's a good habit to learn using tools and skills that are universally applicable as opposed to stuff that's only applicable to one particular task, unless there is some marked advantage to using the more proprietary method of doing things.&lt;br /&gt;
&lt;br /&gt;
If you know of some particularly good reason to use the -l argument instead of piping to grep, by all means let me know here.  --[[User:Jimbo|Jimbo]] 19:23, 9 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The goal is just avoiding to use two processes when only one is needed... More, I tried this :&lt;br /&gt;
 Script started on Sun Jul 10 11:35:19 2005&lt;br /&gt;
 # time portversion -l '&amp;lt;' &amp;gt; /dev/null &lt;br /&gt;
 portversion 1.90s user 0.68s system 86% cpu 3.001 total&lt;br /&gt;
 # time portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
 portversion  2.68s user 0.88s system 69% cpu 5.102 total&lt;br /&gt;
 grep '&amp;lt;' &amp;gt; /dev/null  0.00s user 0.00s system 0% cpu 5.100 total&lt;br /&gt;
 Script done on Sun Jul 10 11:36:23 2005&lt;br /&gt;
&lt;br /&gt;
I do not know how it work internaly, but I gess that the &amp;quot;-l&amp;quot; argument changes the search algorithme.&lt;br /&gt;
&lt;br /&gt;
But I agree with your point of view... grep has to be known by the users !&lt;br /&gt;
The &amp;quot;-l&amp;quot; flag may sometimes be preferable, for a low system for example...&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Yah, if I was scripting repeated operations, by all means I'd use the more efficient way.  But in general I think the modularity of *nix is actually by far the best thing about it, and I try to do things as modular as possible if there isn't a compelling reason to do otherwise.  Even in my own just day-to-day stuff; sure I already know about grep, but still actually using it for everything keeps me in the habit of using it and that's more useful to me than being in the habit of using something that only applies to portversion, y'know? =)&lt;br /&gt;
&lt;br /&gt;
Btw, the difference in efficiency isn't anywhere near as compelling on my systems:&lt;br /&gt;
&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.88s real              0.70s user              0.18s sys&lt;br /&gt;
 ph34r# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         0.89s real              0.68s user              0.21s sys&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion -l '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.03s real              0.83s user              0.18s sys&lt;br /&gt;
 blackbox# /usr/bin/time -h portversion | grep '&amp;lt;' &amp;gt; /dev/null&lt;br /&gt;
         1.04s real              0.83s user              0.19s sys&lt;br /&gt;
&lt;br /&gt;
On looking again at your times, I notice that the CPU% is quite a bit higher on the faster score.  I have a sneaking suspicion a cron job or something kicked in while you were running the second one, so it just didn't get as much processor time and took longer.&lt;br /&gt;
&lt;br /&gt;
--[[User:Jimbo|Jimbo]] 11:52, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
As my load average is about 1.0, I have done my tests 4 or 5 time to check that the result was always the same (it was)... But it may not have incidence on the statistics since they are updating by the kernel when the process is effectively running and collected by the '''time''' command by reading a data structure filled by the ''wait4()'' system call (If I don't remember wrong, '''time''' ''fork()'' itself, ''exec()'' the command and ''wait4()'' for it)...&lt;br /&gt;
&lt;br /&gt;
What I think is that the execution time can be dependant of the number of results : As I might have everyting up to date, i might have the greater difference I can get (all the ports vs. none)...&lt;br /&gt;
&lt;br /&gt;
Well... I have nothing to conclude from this ... All the two methods do the same :)&lt;br /&gt;
--[[User:Smortex|Smortex]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I'm betting it has to do with your high average load.  I generally work with fairly high-powered servers in relatively small environments, with load frequently averaging as low as .05 even during working times.  (PS: when you're writing in talk pages, you might want to use the signature button (looks like a little squiggle up at the top of the textarea) to sign your comments, or if you're using a non-graphical browser, you can get the same effect by using &amp;lt;nowiki&amp;gt;--~~~~&amp;lt;/nowiki&amp;gt; as a code.) =) --[[User:Jimbo|Jimbo]] 18:55, 10 Jul 2005 (EDT)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
05/30/06&lt;br /&gt;
I notice that there is no mention to doing a CVS before using portupgrade; also no mention of the INDEX file being remove.  Should these aspects be addressed?&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
absolutely... address 'em!  --[[User:Jimbo|Jimbo]] 00:14, 31 May 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I just started to use FreeBSD again; so I have to do some research about it before I make a comment.  Thanks Jimbo.  Wed, June 7, 2006 7:51 AM CDT.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Putting the pieces together for portupgrade==&lt;br /&gt;
&lt;br /&gt;
Portupgrade depends on INDEX-6 and INDEX-6.db to function properly. As of release 5.4; INDEX-6 and INDEX-6.db are not part of the download from CVSUP.  You have to build or make the INDEX* files.&lt;br /&gt;
&lt;br /&gt;
1) cvsup -g -L 2 ports-supfile&lt;br /&gt;
&lt;br /&gt;
2) Update the INDEX files via the three methods listed below (a,b,c):&lt;br /&gt;
 a) cd /usr/ports, then &amp;quot;make index&amp;quot;&lt;br /&gt;
 b) cd /usr/ports, then &amp;quot;makefetch index&amp;quot;&lt;br /&gt;
 c) cd /usr/ports, then portsdb -Uu&lt;br /&gt;
3) Then upgrade your ports as described in the methods described above.&lt;br /&gt;
&lt;br /&gt;
A caveat should be mentioned about the methods of obtaining the INDEX files.  Option a) &amp;quot;make index&amp;quot; is officially supported by FreeBSD.  Option b) is supported by FreeBSD; however, please keep in mind that the INDEX files that are retrieved from the servers may be slightly out of date; which may cause build problems.  You may have to change the premission on the download INDEX files via (chmod to match the premissions: &amp;quot;-rw-r--r--&amp;quot;.  Option c) portsdb -Uu was never offically announced as being acceptable or not acceptable.  Portsdb -Uu does create/build and update the INDEX.  &lt;br /&gt;
&lt;br /&gt;
My method of upgrading ports is:&lt;br /&gt;
Step 1, step 2c and then step 3.&lt;br /&gt;
&lt;br /&gt;
The approved methods can be found here as well as the blurb about removing the INDEX files from CVSUP in section 2.5:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/releases/5.4R/relnotes-i386.html&lt;br /&gt;
&lt;br /&gt;
--[[User:68.164.11.90|68.164.11.90]] 15:54, 2 July 2006 (EDT)Hope this helps.&lt;br /&gt;
Pete&lt;br /&gt;
&lt;br /&gt;
: The target is ''fetchindex''.  Portupgrade runs &amp;lt;code&amp;gt;portsdb -u&amp;lt;/code&amp;gt; as part of its routine before building, and between each build.  [[User:Ninereasons|Ninereasons]] 13:53, 4 July 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-30T01:28:34Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: change bullets to sub-heads&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Shell redirection operators include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
=== ''tcsh'' and ''sh'' ===&lt;br /&gt;
# Write output to a file&lt;br /&gt;
#: % mycmd &amp;gt; out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to a file&lt;br /&gt;
#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ''tcsh'' only ===&lt;br /&gt;
# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to out.txt; discard messages&lt;br /&gt;
#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
=== ''sh'' only ===&lt;br /&gt;
# Discard errors, watch output &lt;br /&gt;
#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Append output to out.txt; discard messages&lt;br /&gt;
#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Assign a variable from stored.txt&lt;br /&gt;
#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
# Use all unique lines in stored.txt as an argument variable for ''mycmd'', appending to result.txt&lt;br /&gt;
#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
:: ''-sh: fails without messages''&lt;br /&gt;
:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;/dev/stderr | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;/dev/stdout | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T17:08:44Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: operand (noun) =&amp;gt; operator(verb)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Shell redirection operators include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as an argument variable for ''mycmd'', appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;/dev/stderr | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;/dev/stdout | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T03:24:49Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; */  - perhaps even more clear&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as an argument variable for ''mycmd'', appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;/dev/stderr | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;/dev/stdout | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T03:17:13Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as an argument variable for ''mycmd'', appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T03:15:21Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: consistent terminology&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T02:41:52Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; */  - clarify stream swapping example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to another command. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
because when you switched output to the messages file descriptor, it's done ''right away''.  When you tried to switch errors to output there's nothing left to pipe. It's exactly as though you had said &lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 | command &amp;gt; errors.txt&lt;br /&gt;
''command'' receives no data, because it's all been sent to your screen.  ''errors.txt'' will be empty.  If you had tried it the other way, it's just as though you would have said:&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;&amp;amp;1 | command &amp;gt; errors.txt&lt;br /&gt;
In which case, errors and output are both piped to ''command'', which was not what you wanted.&lt;br /&gt;
 &lt;br /&gt;
This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, like this:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command &amp;gt; errors.txt&lt;br /&gt;
&lt;br /&gt;
Let's step through what this does.&lt;br /&gt;
# ''fd 3'' points to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 2'' points to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;&lt;br /&gt;
# ''fd 1'' points to ''fd 3'' (which is connected to &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt;)&lt;br /&gt;
# ''|'' passes the open file descriptor connected to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt; (''fd 2'') making it &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for ''command''&lt;br /&gt;
# ''&amp;gt;'' redirects the output of ''command'' to ''errors.txt''.&lt;br /&gt;
&lt;br /&gt;
So, what you'll see on your screen is the output of ''myprogram'' on &amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; (not passed to ''command''), followed by errors (if any) from ''command''.  In short, what's been accomplished is, a swap of errors for output, from ''myprogram'' to ''command'', the output of which is redirected to ''errors.txt''.  There is no reliable way to do the same thing in &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T01:40:40Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: filename incorrect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;stored.txt; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T01:35:04Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: better example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ mycmd 2&amp;gt;&amp;amp;1 &amp;gt; out.txt | tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T00:59:29Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T00:54:28Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: correct what is sent to the screen&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-28T00:52:04Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: another equivalent&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt&amp;gt;sorted.txt sort&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/PHP</id>
		<title>PHP</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/PHP"/>
				<updated>2006-06-27T22:50:18Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Installing PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PHP is the recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. Then it is generaly installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.&lt;br /&gt;
&lt;br /&gt;
==Installing PHP==&lt;br /&gt;
&lt;br /&gt;
PHP is available from the port-tree ...&lt;br /&gt;
 # cd /usr/ports/lang/php5&lt;br /&gt;
... and the packages ...&lt;br /&gt;
 # pkg_add -r php5&lt;br /&gt;
&lt;br /&gt;
==Using PHP==&lt;br /&gt;
There are two ways to use PHP :&lt;br /&gt;
* With apache: configure apache to use php to process .php files requested by the user.&lt;br /&gt;
* As a command interpreter: php can process scripts, or instructions typed at the commandline.&lt;br /&gt;
&lt;br /&gt;
===PHP for dynamic websites===&lt;br /&gt;
&lt;br /&gt;
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:&lt;br /&gt;
 AddType application/x-httpd-php .php&lt;br /&gt;
 AddType application/x-httpd-php-source .phps&lt;br /&gt;
&lt;br /&gt;
You will probably also want to add ''index.php'' as a possible directory index:&lt;br /&gt;
 DirectoryIndex index.html index.php&lt;br /&gt;
&lt;br /&gt;
You have to restart Apache to have your changes effective. For example, if you use Apache 2:&lt;br /&gt;
 /usr/local/etc/rc.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Now, you can try your installation by creating a file test.php:&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
    phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Request test.php using your web browser, and if all is well it will show you your PHP configuration.&lt;br /&gt;
&lt;br /&gt;
===Scripts in PHP===&lt;br /&gt;
&lt;br /&gt;
If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the [[shell]]. This is very useful for ''quick and dirty'' hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #!/usr/local/bin/php&lt;br /&gt;
 # phpscript&lt;br /&gt;
 &amp;amp;lt;html&amp;amp;gt; &amp;amp;lt;title&amp;amp;gt;Defined Constants&amp;amp;lt;/title&amp;amp;gt; &amp;amp;lt;body&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;h1&amp;amp;gt;&amp;amp;lt;? echo &amp;quot;Hello from PHP &amp;quot; .phpversion() ?&amp;amp;gt;&amp;amp;lt;/h1&amp;amp;gt;&lt;br /&gt;
 &amp;amp;lt;pre&amp;amp;gt;&amp;amp;lt;? print_r(get_defined_constants()) ?&amp;amp;gt;&amp;amp;lt;/pre&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/body&amp;amp;gt;&amp;amp;lt;/html&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make the file executable:&lt;br /&gt;
 '''%''' chmod +x phpscript&lt;br /&gt;
&lt;br /&gt;
Then, you can run it from the commandline.&lt;br /&gt;
&lt;br /&gt;
 '''%''' ./phpscript | lynx -stdin&lt;br /&gt;
In this case, we've piped to &amp;lt;code&amp;gt;lynx&amp;lt;/code&amp;gt;, so that we can browse the nicely formatted result.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                              Defined Constants (p1 of 154)&lt;br /&gt;
&lt;br /&gt;
                             Hello from PHP 5.1.4&lt;br /&gt;
&lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
    [E_ERROR] =&amp;gt; 1&lt;br /&gt;
    [E_WARNING] =&amp;gt; 2&lt;br /&gt;
    [E_PARSE] =&amp;gt; 4&lt;br /&gt;
    [E_NOTICE] =&amp;gt; 8&lt;br /&gt;
 -- press space for more, use arrow keys to move, '?' for help, 'q' to quit.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, we could have executed the command directly from the commandline:&lt;br /&gt;
 '''%''' php -r 'echo &amp;quot;Hello from PHP &amp;quot;. phpversion() . &amp;quot;\n&amp;quot;; print_r(get_defined_constants());'&lt;br /&gt;
 Hello from PHP 5.1.4 &lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
==PHP Website==&lt;br /&gt;
[http://www.php.net http://www.php.net]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Ports and Packages]]&lt;br /&gt;
[[Category: Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T22:23:42Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Shell pipes and redirection */  - clarify a little&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipelines and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  When the next process inherits the open ''fd'' from the shell the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T21:48:31Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
Also, see the [http://www.freebsd.org/cgi/man.cgi?query=mkfifo&amp;amp;apropos=0&amp;amp;sektion=1&amp;amp;manpath=FreeBSD+6.1-RELEASE&amp;amp;format=html man page] for [[mkfifo]], a utility for creating arbitrary file descriptor files.&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T21:10:55Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: closed file descriptors&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' To close a file descriptor, say &amp;lt;code&amp;gt;n&amp;lt;&amp;amp;-&amp;lt;/code&amp;gt;&lt;br /&gt;
*:: $ mycmd &amp;gt;out.txt 1&amp;lt;&amp;amp;-;&lt;br /&gt;
*:: ''-sh: fails without messages''&lt;br /&gt;
*:: &amp;lt;span style=color:blue &amp;gt;-bash: mycmd: write error: Bad file descriptor&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T21:02:43Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: correct example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:53:37Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: change name of file for example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''local.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''local.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin local.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - local.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:41:44Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: equivalent&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin'. These two commands are equivalent. ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:37:48Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; */  - add prompts&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:36:04Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: generalize example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; mycmd 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;out.txt  | tee err.txt ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:33:07Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: correct example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt;&amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; tiffinfo 001.tif 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;001.out  | tee 001.err ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:24:48Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; */  - add prompts&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: $ exec 3&amp;gt;&amp;amp;1 ; tiffinfo 001.tif 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;001.out  | tee 001.err ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:18:38Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; */  - add prompts&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: % mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: exec 3&amp;gt;&amp;amp;1 ; tiffinfo 001.tif 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;001.out  | tee 001.err ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T20:15:02Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Redirection in bash (or sh) */  - more detail and examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
== Shell pipes and redirection ==&lt;br /&gt;
There are three standard input/output file descriptors (''fd'') that are preconnected to the shell process running on your FreeBSD machine.  Most commands that you would run from the command line expect these file descriptors to be open and accessible. The first (''fd 0'') provides a stream of ''input'' to your program, the second (''fd 1'') provides a stream of ''output'', and the third (''fd 2'') provides a stream of diagnostic messages (usually to your terminal).  &lt;br /&gt;
&lt;br /&gt;
When you open a terminal, before running the shell the terminal device is opened three times to preconnect these file descriptors.  The shell then inherits the file descriptors, and passes them on to each process run from the shell.  &lt;br /&gt;
&lt;br /&gt;
On FreeBSD systems it looks like this:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! file descriptor || Stream  || file descriptor file || device path&lt;br /&gt;
|-&lt;br /&gt;
| '''0''' || Standard input  || &amp;lt;code&amp;gt;/dev/stdin&amp;lt;/code&amp;gt;   || /dev/fd/0&lt;br /&gt;
|-&lt;br /&gt;
| '''1''' || Standard output || &amp;lt;code&amp;gt;/dev/stdout&amp;lt;/code&amp;gt;  || /dev/fd/1&lt;br /&gt;
|-&lt;br /&gt;
| '''2''' || Standard error  || &amp;lt;code&amp;gt;/dev/stderr&amp;lt;/code&amp;gt;  || /dev/fd/2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The example below demonstrates that by default, all of these input/output streams are directed to your terminal (color is added).  &lt;br /&gt;
 $ for i in stdin stdout stderr; do echo $i stuff &amp;gt; /dev/$i; done&lt;br /&gt;
 &amp;lt;span style=color:red  &amp;gt; stdin stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Redirection means that, the file descriptor is temporarily reassigned to somewhere other than the terminal device (a file, a pipe, another file descriptor).  The newly assigned process can reassign the open ''fd'' in turn, so that the stream of data is passed along, for example, by writing to &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, which has been temporarily reassigned to the &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt; for the next process by means of a ''pipe'' ('''|''').  This handing off of open file descriptors by the shell, from one sub-process to another, is called a &amp;quot;shell pipeline&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
There are ''fd n'' (3-9) descriptors potentially available, but only 0-2 are preconnected to the shell; the others must be created from the shell.  In some shells, a standard file descriptor can be detached and reassigned to another file descriptor for as long as the terminal device is open,  using the [[exec]] command (examples appear below).  This capability is just one case among many of why &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; is useful for scripting, and &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; (which lacks it) is not (''see the [http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ FAQ]).&lt;br /&gt;
&lt;br /&gt;
== Redirection in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; compared to &amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The c-shells (&amp;lt;code&amp;gt;tcsh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;csh&amp;lt;/code&amp;gt;) and the Bourne shells (&amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt;) do not handle redirection or piping in quite the same way.  &lt;br /&gt;
&lt;br /&gt;
* '''tcsh and sh'''&lt;br /&gt;
*# Write output to a file&lt;br /&gt;
*#: mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to a file&lt;br /&gt;
*#: mycmd &amp;gt; out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Redirect the output of a remote command to ''remote.txt''.&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; &amp;gt; remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Same command as above, showing only the changes compared to ''remote.txt''.&amp;lt;br /&amp;gt;''Note: Many programs recognize '-' as a shortcut for '/dev/stdin' ''&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff /dev/stdin remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % localcmd &amp;quot;remotecmd&amp;quot; | diff - remote.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Direct &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;+&amp;lt;code&amp;gt;stderr&amp;lt;/code&amp;gt; to file&lt;br /&gt;
*#: % localcmd &amp;gt;&amp;amp; out.txt&lt;br /&gt;
*# Sort lines of jumble.txt into sorted.txt&amp;lt;br /&amp;gt;''Note: the sequence in which redirection appears is not important.  All of the following are exactly equivalent.''&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort &amp;gt;sorted.txt&lt;br /&gt;
*#: % &amp;gt;sorted.txt sort &amp;lt;jumble.txt &lt;br /&gt;
*#: % sort &amp;lt; jumble.txt &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort unique lines of jumble.txt into sorted.txt&lt;br /&gt;
*#: % &amp;lt;jumble.txt sort | uniq &amp;gt;sorted.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Sort ''HERE doc'' delimited by &amp;quot;lines&amp;quot; &lt;br /&gt;
*#: % &amp;lt;&amp;lt;lines sort&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a second line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::? a first line&amp;lt;br /&amp;gt;&lt;br /&gt;
*#::lines&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a first line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:green &amp;gt;a second line&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''tcsh''' only&lt;br /&gt;
*# Discard errors, watch output (probably evil)&amp;lt;br /&amp;gt;''Note: There is no reliable way to do this in tcsh. Here we exploit the fact that terminal reads from &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;.  ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/stdin ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#: % ( mycmd &amp;gt; /dev/tty ) &amp;gt; &amp;amp; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: % (mycmd &amp;gt;&amp;gt; out.txt) &amp;gt;&amp;amp; /dev/null&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be easier in ''tcsh'' - a rare event.  Compare the same task in ''sh''. ''&lt;br /&gt;
*#: % ( mycmd &amp;gt; out.txt ) | &amp;amp; tee err.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* '''sh''' only&lt;br /&gt;
*# Discard errors, watch output &lt;br /&gt;
*#: $  mycmd 2&amp;gt; /dev/null&lt;br /&gt;
*#:: &amp;lt;span style=color:green  &amp;gt; stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Append output to out.txt; discard messages&lt;br /&gt;
*#: $ mycmd 2&amp;gt; /dev/null &amp;gt; out.txt&lt;br /&gt;
*# Write output to out.txt; store and watch errors&amp;lt;br /&amp;gt;''Note: this happens to be harder in ''sh'' - a rare event.  Compare the same task in ''tcsh''. ''&lt;br /&gt;
*#: exec 3&amp;gt;&amp;amp;1 ; tiffinfo 001.tif 2&amp;gt;&amp;amp;1 &amp;gt;&amp;amp;3 1&amp;gt;001.out  | tee 001.err ; exec 1&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Write messages to err.txt; write output to out.txt and copy output to terminal&lt;br /&gt;
*#: $ mycmd 2&amp;gt; err.txt | tee out.txt&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign a variable from stored.txt&lt;br /&gt;
*#: $ &amp;lt;stored.txt read var; mycmd $var &lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Assign first three lines of stored.txt to three different variables&lt;br /&gt;
*#: $ exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; read v1; read v2; read v3; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-; echo $v1 $v2 $v3&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*#:: &amp;lt;span style=color:green&amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*# Use all unique lines in stored.txt as variable input, appending to result.txt&lt;br /&gt;
*#: exec 3&amp;lt;&amp;amp;0; exec &amp;lt;test; sort | uniq | while read line; do mycmd $line &amp;gt;&amp;gt; result.txt ; done; exec 0&amp;lt;&amp;amp;3 3&amp;lt;&amp;amp;-;&lt;br /&gt;
*#:: &amp;lt;span style=color:blue&amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed right represents &amp;lt;code&amp;gt;stdout&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 1&amp;gt; out.txt&lt;br /&gt;
*:: $ mycmd &amp;gt; out.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*: ''Note:'' By default, redirection pointed left represents &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;, so that these two commands are exactly equivalent:&lt;br /&gt;
*:: $ mycmd 0&amp;lt; in.txt&lt;br /&gt;
*:: $ mycmd &amp;lt; in.txt&lt;br /&gt;
*::: &amp;lt;span style=color:blue &amp;gt;stderr stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
*::: &amp;lt;span style=color:green &amp;gt;stdout stuff&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A little more about ''fd n'' in &amp;lt;code&amp;gt;sh&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Redirection</id>
		<title>Redirection</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Redirection"/>
				<updated>2006-06-27T17:34:50Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: edit description of redirectors&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Redirection''' is sending the output of a program to somewhere other than where it would otherwise go - for example you can '''redirect''' the output of an [[ls]] command to a text file for later processing or to the [[grep]] command for filtering.  Common operands include: '''&amp;gt;''', '''&amp;gt;&amp;gt;''', '''&amp;lt;''', '''&amp;lt;&amp;lt;''', and the ever popular '''|'''.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;  sends output to a file (may include [[special files]] such as [[/dev/null]])&lt;br /&gt;
 &amp;gt;&amp;gt; appends output to a file (without overwriting it)&lt;br /&gt;
 &amp;lt;  read file to &amp;lt;code&amp;gt;stdin&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;lt; read to stdin from &amp;lt;code&amp;gt;&amp;lt;&amp;lt;delimiter&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;delimiter&amp;lt;/code&amp;gt; (a ''HERE doc'').&lt;br /&gt;
  |  sends output to a program (frequently, a [[:Category:System Commands|system command]] like [[grep]])&lt;br /&gt;
&lt;br /&gt;
If you're using the [[bash]] or [[bourne shell|bourne]] shells, you also have some special options available to you: you can redirect standard input, standard output and standard error messages with far greater flexibility and reliability.  Other shells such as [[csh]] are notably limited in redirection capability, making them better suited to interactive use than to [[shell programming]] or other complex uses.&lt;br /&gt;
&lt;br /&gt;
==Redirection in bash (or sh)==&lt;br /&gt;
Bash's and sh's shining moment comes for users who need to see and/or redirect errors to somewhere other than STOUT (your console). Standard input (STIN), standard output (STOUT) and standard error (STERR) are by default sent to the same place: STOUT (on most systems, this will be your console or tty). [[bash]] labels these descriptors 0, 1 and 2, respectively -- you have 10 descriptors, but only 0-2 are actually taken by anything. So&lt;br /&gt;
 samizdata# myprogram &amp;gt; file&lt;br /&gt;
will send the output of ''myprogram'' to ''file''&lt;br /&gt;
 samizdata# myprogram 2&amp;gt; file &lt;br /&gt;
will send only the '''error''' output of ''myprogram'' to ''file''&lt;br /&gt;
 samizdata# myprogram 2&amp;gt;&amp;amp;1 | command2&lt;br /&gt;
will send errors (descriptor 2,) to the same place as output (descriptor 1) and then pipe that to ''command2''&lt;br /&gt;
&lt;br /&gt;
Okay, let's say you want to send output to your screen and errors to a file. You '''can't''' just do&lt;br /&gt;
 samizdata# myprogram 1&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 &amp;gt; errors.txt&lt;br /&gt;
because when you do the first switch, it's done ''right away'' and when the second &amp;gt;&amp;amp; comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:&lt;br /&gt;
 samizdata# myprogram 3&amp;gt;&amp;amp;2 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 | command&lt;br /&gt;
will make the output of ''myprogram'' do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to ''command''&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]] [[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/PHP</id>
		<title>PHP</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/PHP"/>
				<updated>2006-06-23T00:35:07Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Using PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PHP is the recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. Then it is generaly installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.&lt;br /&gt;
&lt;br /&gt;
==Installing PHP==&lt;br /&gt;
&lt;br /&gt;
PHP is available trougth the port-tree ...&lt;br /&gt;
 # cd /usr/ports/lang/php5&lt;br /&gt;
... and the packages ...&lt;br /&gt;
 # pkg_add -r php5&lt;br /&gt;
&lt;br /&gt;
==Using PHP==&lt;br /&gt;
There are two ways to use PHP :&lt;br /&gt;
* With apache: configure apache to use php to process .php files requested by the user.&lt;br /&gt;
* As a command interpreter: php can process scripts, or instructions typed at the commandline.&lt;br /&gt;
&lt;br /&gt;
===PHP for dynamic websites===&lt;br /&gt;
&lt;br /&gt;
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:&lt;br /&gt;
 AddType application/x-httpd-php .php&lt;br /&gt;
 AddType application/x-httpd-php-source .phps&lt;br /&gt;
&lt;br /&gt;
You will probably also want to add ''index.php'' as a possible directory index:&lt;br /&gt;
 DirectoryIndex index.html index.php&lt;br /&gt;
&lt;br /&gt;
You have to restart Apache to have your changes effective. For example, if you use Apache 2:&lt;br /&gt;
 /usr/local/etc/rc.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Now, you can try your installation by creating a file test.php:&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
    phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Request test.php using your web browser, and if all is well it will show you your PHP configuration.&lt;br /&gt;
&lt;br /&gt;
===Scripts in PHP===&lt;br /&gt;
&lt;br /&gt;
If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the [[shell]]. This is very useful for ''quick and dirty'' hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #!/usr/local/bin/php&lt;br /&gt;
 # phpscript&lt;br /&gt;
 &amp;amp;lt;html&amp;amp;gt; &amp;amp;lt;title&amp;amp;gt;Defined Constants&amp;amp;lt;/title&amp;amp;gt; &amp;amp;lt;body&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;h1&amp;amp;gt;&amp;amp;lt;? echo &amp;quot;Hello from PHP &amp;quot; .phpversion() ?&amp;amp;gt;&amp;amp;lt;/h1&amp;amp;gt;&lt;br /&gt;
 &amp;amp;lt;pre&amp;amp;gt;&amp;amp;lt;? print_r(get_defined_constants()) ?&amp;amp;gt;&amp;amp;lt;/pre&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/body&amp;amp;gt;&amp;amp;lt;/html&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make the file executable:&lt;br /&gt;
 '''%''' chmod +x phpscript&lt;br /&gt;
&lt;br /&gt;
Then, you can run it from the commandline.&lt;br /&gt;
&lt;br /&gt;
 '''%''' ./phpscript | lynx -stdin&lt;br /&gt;
In this case, we've piped to &amp;lt;code&amp;gt;lynx&amp;lt;/code&amp;gt;, so that we can browse the nicely formatted result.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                              Defined Constants (p1 of 154)&lt;br /&gt;
&lt;br /&gt;
                             Hello from PHP 5.1.4&lt;br /&gt;
&lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
    [E_ERROR] =&amp;gt; 1&lt;br /&gt;
    [E_WARNING] =&amp;gt; 2&lt;br /&gt;
    [E_PARSE] =&amp;gt; 4&lt;br /&gt;
    [E_NOTICE] =&amp;gt; 8&lt;br /&gt;
 -- press space for more, use arrow keys to move, '?' for help, 'q' to quit.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, we could have executed the command directly from the commandline:&lt;br /&gt;
 '''%''' php -r 'echo &amp;quot;Hello from PHP &amp;quot;. phpversion() . &amp;quot;\n&amp;quot;; print_r(get_defined_constants());'&lt;br /&gt;
 Hello from PHP 5.1.4 &lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
==PHP Website==&lt;br /&gt;
[http://www.php.net http://www.php.net]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Ports and Packages]]&lt;br /&gt;
[[Category: Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/PHP</id>
		<title>PHP</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/PHP"/>
				<updated>2006-06-23T00:34:13Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* PHP for dynamic websites */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PHP is the recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. Then it is generaly installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.&lt;br /&gt;
&lt;br /&gt;
==Installing PHP==&lt;br /&gt;
&lt;br /&gt;
PHP is available trougth the port-tree ...&lt;br /&gt;
 # cd /usr/ports/lang/php5&lt;br /&gt;
... and the packages ...&lt;br /&gt;
 # pkg_add -r php5&lt;br /&gt;
&lt;br /&gt;
==Using PHP==&lt;br /&gt;
There are two ways to use PHP :&lt;br /&gt;
* With apache: configure apache to use php to process .php files requested by the user.&lt;br /&gt;
* As command interpreter: php can process scripts, or instructions typed at the commandline.&lt;br /&gt;
&lt;br /&gt;
===PHP for dynamic websites===&lt;br /&gt;
&lt;br /&gt;
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:&lt;br /&gt;
 AddType application/x-httpd-php .php&lt;br /&gt;
 AddType application/x-httpd-php-source .phps&lt;br /&gt;
&lt;br /&gt;
You will probably also want to add ''index.php'' as a possible directory index:&lt;br /&gt;
 DirectoryIndex index.html index.php&lt;br /&gt;
&lt;br /&gt;
You have to restart Apache to have your changes effective. For example, if you use Apache 2:&lt;br /&gt;
 /usr/local/etc/rc.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Now, you can try your installation by creating a file test.php:&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
    phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Request test.php using your web browser, and if all is well it will show you your PHP configuration.&lt;br /&gt;
&lt;br /&gt;
===Scripts in PHP===&lt;br /&gt;
&lt;br /&gt;
If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the [[shell]]. This is very useful for ''quick and dirty'' hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #!/usr/local/bin/php&lt;br /&gt;
 # phpscript&lt;br /&gt;
 &amp;amp;lt;html&amp;amp;gt; &amp;amp;lt;title&amp;amp;gt;Defined Constants&amp;amp;lt;/title&amp;amp;gt; &amp;amp;lt;body&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;h1&amp;amp;gt;&amp;amp;lt;? echo &amp;quot;Hello from PHP &amp;quot; .phpversion() ?&amp;amp;gt;&amp;amp;lt;/h1&amp;amp;gt;&lt;br /&gt;
 &amp;amp;lt;pre&amp;amp;gt;&amp;amp;lt;? print_r(get_defined_constants()) ?&amp;amp;gt;&amp;amp;lt;/pre&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/body&amp;amp;gt;&amp;amp;lt;/html&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make the file executable:&lt;br /&gt;
 '''%''' chmod +x phpscript&lt;br /&gt;
&lt;br /&gt;
Then, you can run it from the commandline.&lt;br /&gt;
&lt;br /&gt;
 '''%''' ./phpscript | lynx -stdin&lt;br /&gt;
In this case, we've piped to &amp;lt;code&amp;gt;lynx&amp;lt;/code&amp;gt;, so that we can browse the nicely formatted result.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                              Defined Constants (p1 of 154)&lt;br /&gt;
&lt;br /&gt;
                             Hello from PHP 5.1.4&lt;br /&gt;
&lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
    [E_ERROR] =&amp;gt; 1&lt;br /&gt;
    [E_WARNING] =&amp;gt; 2&lt;br /&gt;
    [E_PARSE] =&amp;gt; 4&lt;br /&gt;
    [E_NOTICE] =&amp;gt; 8&lt;br /&gt;
 -- press space for more, use arrow keys to move, '?' for help, 'q' to quit.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, we could have executed the command directly from the commandline:&lt;br /&gt;
 '''%''' php -r 'echo &amp;quot;Hello from PHP &amp;quot;. phpversion() . &amp;quot;\n&amp;quot;; print_r(get_defined_constants());'&lt;br /&gt;
 Hello from PHP 5.1.4 &lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
==PHP Website==&lt;br /&gt;
[http://www.php.net http://www.php.net]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Ports and Packages]]&lt;br /&gt;
[[Category: Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/PHP</id>
		<title>PHP</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/PHP"/>
				<updated>2006-06-23T00:33:26Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Using PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PHP is the recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. Then it is generaly installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.&lt;br /&gt;
&lt;br /&gt;
==Installing PHP==&lt;br /&gt;
&lt;br /&gt;
PHP is available trougth the port-tree ...&lt;br /&gt;
 # cd /usr/ports/lang/php5&lt;br /&gt;
... and the packages ...&lt;br /&gt;
 # pkg_add -r php5&lt;br /&gt;
&lt;br /&gt;
==Using PHP==&lt;br /&gt;
There are two ways to use PHP :&lt;br /&gt;
* With apache: configure apache to use php to process .php files requested by the user.&lt;br /&gt;
* As command interpreter: php can process scripts, or instructions typed at the commandline.&lt;br /&gt;
&lt;br /&gt;
===PHP for dynamic websites===&lt;br /&gt;
&lt;br /&gt;
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:&lt;br /&gt;
 AddType application/x-httpd-php .php&lt;br /&gt;
 AddType application/x-httpd-php-source .phps&lt;br /&gt;
&lt;br /&gt;
You will probably also want to add ''index.php'' as a possible directory index:&lt;br /&gt;
 DirectoryIndex index.html index.php&lt;br /&gt;
&lt;br /&gt;
You have to restart Apache to have your changes effective. For example, if you use Apache 2:&lt;br /&gt;
 /usr/local/etc/rc.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Now, you can try your installation by creating a file test.php:&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
    phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Request test.php using your web browser, and if all is well it will show you your php settings.&lt;br /&gt;
&lt;br /&gt;
===Scripts in PHP===&lt;br /&gt;
&lt;br /&gt;
If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the [[shell]]. This is very useful for ''quick and dirty'' hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #!/usr/local/bin/php&lt;br /&gt;
 # phpscript&lt;br /&gt;
 &amp;amp;lt;html&amp;amp;gt; &amp;amp;lt;title&amp;amp;gt;Defined Constants&amp;amp;lt;/title&amp;amp;gt; &amp;amp;lt;body&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;h1&amp;amp;gt;&amp;amp;lt;? echo &amp;quot;Hello from PHP &amp;quot; .phpversion() ?&amp;amp;gt;&amp;amp;lt;/h1&amp;amp;gt;&lt;br /&gt;
 &amp;amp;lt;pre&amp;amp;gt;&amp;amp;lt;? print_r(get_defined_constants()) ?&amp;amp;gt;&amp;amp;lt;/pre&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/body&amp;amp;gt;&amp;amp;lt;/html&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make the file executable:&lt;br /&gt;
 '''%''' chmod +x phpscript&lt;br /&gt;
&lt;br /&gt;
Then, you can run it from the commandline.&lt;br /&gt;
&lt;br /&gt;
 '''%''' ./phpscript | lynx -stdin&lt;br /&gt;
In this case, we've piped to &amp;lt;code&amp;gt;lynx&amp;lt;/code&amp;gt;, so that we can browse the nicely formatted result.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                              Defined Constants (p1 of 154)&lt;br /&gt;
&lt;br /&gt;
                             Hello from PHP 5.1.4&lt;br /&gt;
&lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
    [E_ERROR] =&amp;gt; 1&lt;br /&gt;
    [E_WARNING] =&amp;gt; 2&lt;br /&gt;
    [E_PARSE] =&amp;gt; 4&lt;br /&gt;
    [E_NOTICE] =&amp;gt; 8&lt;br /&gt;
 -- press space for more, use arrow keys to move, '?' for help, 'q' to quit.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, we could have executed the command directly from the commandline:&lt;br /&gt;
 '''%''' php -r 'echo &amp;quot;Hello from PHP &amp;quot;. phpversion() . &amp;quot;\n&amp;quot;; print_r(get_defined_constants());'&lt;br /&gt;
 Hello from PHP 5.1.4 &lt;br /&gt;
 Array&lt;br /&gt;
 (&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
==PHP Website==&lt;br /&gt;
[http://www.php.net http://www.php.net]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Ports and Packages]]&lt;br /&gt;
[[Category: Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Regular_expressions</id>
		<title>Regular expressions</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Regular_expressions"/>
				<updated>2006-06-22T18:39:20Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: egrep&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Regular expressions''' are patterns that are used to describe a string of characters. Regular expressions might be used to match substrings in text as a logical condition for doing something in a program, for capturing the text matched by the expression in order to do something with it, and many other uses.  Users more familiar with Windows might think of regular expressions as comparable to &amp;quot;wildcards&amp;quot;, only wilder.&lt;br /&gt;
&lt;br /&gt;
From the commandline and in [[shell script]]s, the stream editor ([[sed]]) is used to make substitutions for matched text.  &amp;lt;code&amp;gt;egrep&amp;lt;/code&amp;gt; (shorthand for &amp;lt;code&amp;gt;grep -E&amp;lt;/code&amp;gt;) filters strings using regular expressions.   For more complex types of pattern matching and text manipulation, [[Perl]] and [[Tcl]] have a built-in regular expression engine; and most modern programming languages have regular expression modules.&lt;br /&gt;
&lt;br /&gt;
== external links ==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Regular_expression Wikipedia entry]&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Talk:DIR</id>
		<title>Talk:DIR</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Talk:DIR"/>
				<updated>2006-06-21T18:44:20Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* grep &amp;gt; simple wildcarding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Best in what sense?==&lt;br /&gt;
&lt;br /&gt;
:''ls is best combined with grep if you plan on doing any filtering: dir *.txt is equivalent to ls | grep .txt, and dir boogy*.* is equivalent to ls | grep boogy.''&lt;br /&gt;
&lt;br /&gt;
What is &amp;quot;best&amp;quot; about this? [[User:Ninereasons|Ninereasons]] 20:02, 19 June 2006 (EDT)&lt;br /&gt;
&lt;br /&gt;
== grep &amp;gt; simple wildcarding ==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;best&amp;quot; in the sense that you get far more fine grained control out of piping to grep than you would with the use of simple wildcards.&lt;br /&gt;
&lt;br /&gt;
: Ok. But how do the examples illustrate this? [[User:Ninereasons|Ninereasons]] 14:44, 21 June 2006 (EDT)&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Daemon</id>
		<title>Daemon</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Daemon"/>
				<updated>2006-06-20T21:43:08Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: /* Prevent daemonic mischief */  more daemonic scariness&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A '''daemon''' is a program that runs in the background, rather than under the direct control of a user.  If you are more familiar with DOS or early Windows, this kind of program would be known to you as a ''TSR'' (''Terminate and Stay Resident'') program.  In NT systems, you would refer to these as ''[[services]]''.  Unix users might use the name ''daemon'', ''[[background processes]]'' and ''service'' with rough equivalence, especially when speaking to a general audience. &lt;br /&gt;
&lt;br /&gt;
In FreeBSD, programs that are run as daemons will (usually) have a &amp;quot;d&amp;quot; appended to their name, e.g., [[sshd]], [[named]], etc.  Loosely speaking, a ''daemon'' is any process that runs in the background (instead of interactively).  If an interactive process such as a shell is used to start the ''daemon'', the ''daemon'' continues to run after the interactive process is closed.  In FreeBSD, ''daemons'' are ordinarily started by [[init]] at bootup, and stopped by [[init]] at shutdown.  &lt;br /&gt;
&lt;br /&gt;
The name comes from the old Greek (daimon, ''&amp;amp;delta;&amp;amp;alpha;&amp;amp;iota;&amp;amp;mu;&amp;amp;omega;&amp;amp;nu;''), referring to inferior spirits that do things the gods would rather not be concerned with - like do work.  &lt;br /&gt;
&lt;br /&gt;
==Prevent daemonic mischief==&lt;br /&gt;
It matters when and how a ''daemon'' is started.  Carelessly started, a ''daemon'' might leak information to the world about your username and other information or capabilities exported from your login environment, provide the world with access to underlying instances of the main ''daemon'' process, bog down or stop your machine with too many instances of itself, open so many file descriptors that you are prevented even from rebooting the machine, serve as a proxy for the malicious use of other programs, and so on.  Daemons aren't usually evil; they're just misunderstood.  Be aware of the [[:Category:Securing FreeBSD|security implications]] of your configuration and startup options.&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD Terminology]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Rc.subr</id>
		<title>Rc.subr</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Rc.subr"/>
				<updated>2006-06-20T20:41:23Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''rc.subr''' is a subroutine library that was developed primarily for the NetBSD system and imported into FreeBSD where it was further developed.  See [http://www.freebsd.org/cgi/man.cgi?query=rc.subr&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html man rc.subr] &lt;br /&gt;
&lt;br /&gt;
Under FreeBSD 4.x, you may have noticed this file being installed into ''/usr/local/etc/rc.subr'', where it was increasingly used by port-installed daemons.  Beginning in FreeBSD 5.0, this file became integral to the FreeBSD startup mechanism for both, base-installed services and port-installed services.  At the same time, the directory ''/etc/rc.d'' appeared in FreeBSD, where you will find startup scripts for all base-installed services (so that, for example, ''/etc/rc.sendmail'' disappeared, and was replaced by ''/[[etc/rc.d/sendmail]]'', which uses the ''rc.subr'' mechanism)&lt;br /&gt;
&lt;br /&gt;
Besides being a repository of some very useful and well-written functions, perhaps the chief advantage of using the file is that it ties the configuration, startup and shutdown of these daemons to /[[etc/rc.conf]] in a consistent way.  For example, unless the service has an &amp;lt;code&amp;gt;=&amp;quot;YES&amp;quot;&amp;lt;/code&amp;gt; line in ''rc.conf'', the ''start'' and ''stop'' commands have no effect.  &lt;br /&gt;
&lt;br /&gt;
[[User:Jimbo|Some folks]] don't like starting all of their services from ''/etc/rc.conf''.  ''' ''rc.subr'' ''' accommodates this preference with the prefixes, ''force'' and ''one''.&lt;br /&gt;
&lt;br /&gt;
Another significant change that this mechanism introduces is, if a startup script ends with ''.sh'', it will be loaded into the current shell (instead of a subshell); ''' ''N.B.'' ''': this means that ''if ''script.sh'' fails'', the remainder of the startup sequence will not be executed.&lt;br /&gt;
&lt;br /&gt;
Finally, a valuable improvement to service startup is better control over the order in which [[daemon]]s are started.  See [http://www.freebsd.org/cgi/man.cgi?query=rc&amp;amp;sektion=8&amp;amp;apropos=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports man rc(8)] and [http://www.freebsd.org/cgi/man.cgi?query=rcorder&amp;amp;sektion=8&amp;amp;apropos=0&amp;amp;manpath=FreeBSD+6.0-stable man rcorder(8)]&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
1. Minimal ''rc.d/'' script example, using the new ''rc.subr'' mechanism.  Most scripts require little more than this.&lt;br /&gt;
   #!/bin/sh&lt;br /&gt;
   #&lt;br /&gt;
   # PROVIDE: foo&lt;br /&gt;
   # REQUIRE: bar_service_required_to_precede_foo&lt;br /&gt;
   # BEFORE:  baz_service_requiring_foo_to_precede_it&lt;br /&gt;
   &lt;br /&gt;
   . /etc/rc.subr&lt;br /&gt;
   &lt;br /&gt;
   name=&amp;quot;foo&amp;quot;&lt;br /&gt;
   rcvar=`set_rcvar`&lt;br /&gt;
   command=&amp;quot;/usr/local/bin/foo&amp;quot;&lt;br /&gt;
   load_rc_config $name&lt;br /&gt;
   run_rc_command &amp;quot;$1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
2. Manually start and stop mysql, even though &amp;lt;code&amp;gt;mysql_enable&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;&amp;quot;NO&amp;quot;&amp;lt;/code&amp;gt; (default) in ''rc.conf''.  &lt;br /&gt;
* Skip all failing pre-requisite &amp;quot;''required_*''&amp;quot; tests (if any).&lt;br /&gt;
 # /usr/local/etc/rc.d/mysql-server forcestart&lt;br /&gt;
 # /usr/local/etc/rc.d/mysql-server forcestop&lt;br /&gt;
* Skip only the &amp;lt;code&amp;gt;=&amp;quot;YES&amp;quot;&amp;lt;/code&amp;gt; test&lt;br /&gt;
 # /usr/local/etc/rc.d/mysql-server onestart&lt;br /&gt;
 # /usr/local/etc/rc.d/mysql-server onestop&lt;br /&gt;
&lt;br /&gt;
3. List the sequence in which all services will be started, reporting missing dependencies:&lt;br /&gt;
 # rcorder /etc/rc.d/* /usr/local/etc/rc.d/* &lt;br /&gt;
&lt;br /&gt;
[[Category:Important Config Files]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Usr/local/etc/rc.d</id>
		<title>Usr/local/etc/rc.d</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Usr/local/etc/rc.d"/>
				<updated>2006-06-20T20:40:51Z</updated>
		
		<summary type="html">&lt;p&gt;Ninereasons: Moving section to a separate article&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''/usr/local/etc/rc.d''' isn't actually a config file, it's a directory.  Any executable shell script in this directory will be executed (using the [[Bourne shell]]) with the argument &amp;quot;start&amp;quot; when the system boots, and again with the argument &amp;quot;stop&amp;quot; when the system is shut down.&lt;br /&gt;
&lt;br /&gt;
You may find this generic rc.d script (shown as I use it to start bind9) handy for general purpose use:&lt;br /&gt;
&lt;br /&gt;
 #!/bin/sh&lt;br /&gt;
 &lt;br /&gt;
 case &amp;quot;$1&amp;quot; in&lt;br /&gt;
 start)&lt;br /&gt;
         /usr/sbin/named -c /etc/namedb/named.conf &amp;amp;&lt;br /&gt;
         echo &amp;quot;bind9&amp;quot;&lt;br /&gt;
         exit 0&lt;br /&gt;
         ;;&lt;br /&gt;
 stop)&lt;br /&gt;
         exec killall named&lt;br /&gt;
         ;;&lt;br /&gt;
 *)&lt;br /&gt;
         echo &amp;quot;Usage: `basename $0` {start|stop}&amp;quot; &amp;gt;&amp;amp;2&lt;br /&gt;
         exit 64&lt;br /&gt;
         ;;&lt;br /&gt;
 esac&lt;br /&gt;
&lt;br /&gt;
Note that scripts in /usr/local/etc/rc.d cannot and will not execute, at boot time or otherwise, if you do not set the permissions to allow their execution.  In most cases, you will want to [[chmod]] 755 any rc.d scripts, though you may also consider 700 on root-owned rc.d scripts to make sure that unprivileged users don't mess with attempting to start or stop sensitive services.&lt;br /&gt;
&lt;br /&gt;
The most recent versions of FreeBSD make [[rc.conf]] more integral to the startup of local services. For information about the way that [[daemon]]s are started in the newest versions of FreeBSD, see [[rc.subr]]. &lt;br /&gt;
&lt;br /&gt;
[[Category:Important Config Files]]&lt;/div&gt;</summary>
		<author><name>Ninereasons</name></author>	</entry>

	</feed>