home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Tools / nShell™ 1.0.3 / nShell / User's Guide / 08 Input⁄Output Redirection < prev    next >
Encoding:
Text File  |  1994-09-04  |  3.8 KB  |  123 lines  |  [TEXT/ttxt]

  1.  
  2. 08 Input/Output Redirection
  3. ===========================
  4.  
  5. One of the principal strengths of shell environments, is the way that several small commands can be linked together to provide greater functionality.  Input/Output Redirection is the process by which a user may direct the input or output of a command to a file, or even another command. 
  6.  
  7. Straight Pipes
  8. --------------
  9.  
  10. Straight pipes take the output of one command and pass it to another. The second command, rather than looking for input from the keyboard, takes input from the first command.  An example of a simple pipe would be:
  11.  
  12.     hello | notify
  13.  
  14. The "|" character is used to specify a pipe.  In this example the hello command produces the string "Hello World", which is then sent to the notify command.  The notify command pops a dialog containing the "Hello World" string.
  15.  
  16. A longer series of commands could be piped together:
  17.  
  18.     hello | wc | notify
  19.  
  20. In this case the "Hello World" string is sent to wc, a word count command, and the result of wc is sent to notify.
  21.  
  22. Redirecting To And From Files
  23. -----------------------------
  24.  
  25. The inputs and outputs of commands can be directed to files.  For example, the command:
  26.  
  27.     hello >hello.txt
  28.  
  29. directs the output of the hello command to a file called hello.txt.
  30.  
  31. In a similar way, inputs to commands can be directed to files.  For example, the command:
  32.  
  33.     wc <hello.txt
  34.  
  35. directs the wc command to take its input from the file called hello.txt.
  36.  
  37. Input and output may be redirected at the same time, the command:
  38.  
  39.     wc <hello.txt >wc.txt
  40.  
  41. directs the wc command to take its input from hello.txt and to write its output to wc.txt.
  42.  
  43. Adding To Existing Files
  44. ------------------------
  45.  
  46. The '>' redirector creates a new file to hold the command's output.  If a file exists, it will be overwritten.  The '>>' redirector tells the shell to "add to" the existing file:
  47.  
  48.     hello >>hello.txt
  49.  
  50. Standard Error
  51. --------------
  52.  
  53. To prevent errors from being hidden, the nShell splits output into two streams "Standard Output" and "Standard Error".  Consider the example:
  54.  
  55.     % man >man.txt
  56.     Usage: man <command or keyword>.
  57.  
  58. In this case an error message was generated and written to "Standard Error".  If we wanted to direct the error message to a file we would use the "&" character:
  59.  
  60.     man >&man.out
  61.  
  62. The "&" character can be used with the ">>" redirector also, as in:
  63.  
  64.     man >>&man.out
  65.  
  66. Special Devices
  67. ---------------
  68.  
  69. Two special output devices are available within the nShell:
  70.  
  71. dev:null    Discard output
  72. dev:tty     Redirect to the nShell window
  73.  
  74. The first device, dev:null, is useful when you wish to run a command, but are not interested in its output.  Consider the "chattr" command used to modify a file's creator or type:
  75.  
  76.     % chattr script.txt -c john
  77.     Crea   Type  Name
  78.     ------ ------ ----
  79.     'john' 'TEXT' script.txt
  80.  
  81. By redirecting the result of this command to dev:null, the output can be suppressed:
  82.  
  83.     % chattr script.txt -c john >dev:null
  84.  
  85. The second device, dev:tty, is useful when overriding a previous output redirection.  Consider the script:
  86.  
  87.     echo "hello one"
  88.     echo "hello two" >dev:tty
  89.     echo "hello three"
  90.  
  91. If no redirection is performed, the output of this script would be:
  92.  
  93.     % script
  94.     hello one
  95.     hello two
  96.     hello three
  97.  
  98. If we were to try redirecting the output to a file, the second line would still appear on the console:
  99.  
  100.     % script >script.txt
  101.     hello two
  102.  
  103. And the file "script.txt" would contain:
  104.  
  105.     hello one
  106.     hello three
  107.  
  108. Use dev:tty when you want to make sure that a message goes to the console.
  109.  
  110. Protecting Characters
  111. ---------------------
  112.  
  113. As you can see above, the shell assumes that ">" and "<" characters are requesting redirection.  This could result in some strange effects.  The command
  114.  
  115.     notify <oops!>
  116.  
  117. would mean take input from a file called "oops!>".  (The input filename is terminated by the first space.).
  118.  
  119. Protecting redirection characters with quotes:
  120.  
  121.     notify "<oops!>"
  122.  
  123. would have the desired results.