DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OSXFAQ Mac OS X UNIX Tip-of-the-Day  back to index

Scripting One-Liners - Bulk Redirection

By Adrian Mayo - Editor - OSXFAQ

We all know how to redirect output to a file using the redirection symbol '>'.

$ ls > file-list

Here's an example in which we redirect the output of many commands in a shell script, and without having to redirect the output of each command individually. Create a sub-shell around the commands who's output you wish to redirect.

$ (
>ls
>df
>cat number-list
>) > out
$

You can use a group-command instead of a sub-shell by replacing

(...) > out

with

{...} > out

Use the same trick to re-direct standard input to a group of commands.

$ cat infile
1234
full
$ { read code; read membership; } < infile
$ echo $code $membership
1234 full
$

Note: use a group-command, not a sub-shell, if you wish to retain the values of variables. A sub-shell is executed by a new instance of the shell, and its environment (including all variable) dies when the sub-shell completes.


Visit the Site of the Book of the Unix Tips:
Project 85 covers sub-shells and gives detailed examples of the redirection and many other tricks.


Discuss this trick in the OSXFAQ Learning Center forum

E-mail your comments or suggestions to webmaster@osxfaq.com


Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.