| By Dave Taylor | Article Rating: |
|
| July 27, 2004 12:00 AM EDT | Reads: |
12,669 |
In a lot of ways, Linux seems pretty similar to other modern operating systems, with overlapping windows, a mouse, games, an e-mail program that talks to Outlook, an application that can read and write Microsoft Word documents, etc. What's different, though, is that Linux is built atop a very powerful underlying kernel that supports a wide variety of hardware and offers all users access to its power without any of the graphical layers that get in the way of real power users.
That's what this column is all about: cracking open the shell, learning how you can really exploit the power and capabilities of the command line, and how you can dip your toe into the water and learn about programming basics through the painless world of Linux shell scripting. Come on in: the water's just fine...
Redirection
One of the greatest capabilities of the shell, and one of the truly great inventions of Unix (then reimplemented in Linux), is file redirection. But redirection offers quite a bit more power than you may be used to.The most basic redirection is to use > to point the output to a specified filename. This looks like:
ls > my-files
This saves the output of the ls command to a file called "my-files". If you want to add something else to the file, this is known in Linux circles as appending content, and that's done by using >> rather than just a single angle bracket:
date >> my-files
This adds the output of the date command to the existing contents of "my-files". Be careful, though; it doesn't know how to add a few blank lines between content blocks or anything else. It's crude and direct, literally opening up the file and streaming in the content.
You can also redirect input, which is very useful too. For example, to find out how many words are in the new file, use the wc command with the "-w" flag:
wc -w < my-files
48
This shows that there are 48 "words" in the file (in this case digits with spaces around them are also counted as words, so it's not that useful a result in this particular instance).
More Sophisticated Uses
A more sophisticated use of shell redirection is to use what's known as a here document, which is best explained by demonstrating it:wc -w << ENDMARK
This is a test of what I would
ordinarily have in a file but am
instead streaming from within
this script.
ENDMARK
21
Can you see what's happened here? I've used a here document to make an instant file that I've then fed to the wc command. Once the command has processed its input, though, the file contents vanish and nothing's left behind.
This proves to be a tremendously useful technique in shell scripting because we can, for example, create 3–5 line mini-scripts for specific Linux commands within a shell script, so we don't have to worry about a bunch of files all being available when they can easily be created on-the-fly.
These are the basics of file redirection, at least until we consider the difference between stdin, stdout, and stderr, which we'll explore in the next column. In the meantime, thanks for reading and please e-mail me with any and all questions you have or topics you'd like to see me discuss here!
Published July 27, 2004 Reads 12,669
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Dave Taylor
Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.
![]() |
Byron Rendar 07/29/04 11:00:54 AM EDT | |||
Dave's use of redirecting input was rather weak. In his example with wc -w there was no need to use input redirection, so why bother to type the extra "<". |
||||
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Linux Virtualization and Tired Open Source Myths
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- Amazon Kindle Fire Gets Its Own 'Personal Cloud Desktop' with AlwaysOnPC App Launch
- SPIRIT DSP Receives 2011 INTERNET TELEPHONY Product of the Year Award
- Hadoop Quickstart: Use Whirr to automate standup of your distributed cluster on Rackspace
- Jury Gets Novell Antitrust Case Against Microsoft
- The Utility Infrastructure Security Market 2012-2022: Cybersecurity & Smart Grids
- FORTUNE Magazine Names Rackspace Among “100 Best Companies to Work For”
- iFollowOffice Turns to Virtual Bridges and Savvis for On-Demand Virtual Desktop Services
- EnterpriseDB Announces Availability of Postgres Plus Cloud Database
- i-Technology in 2012: Five Industry Predictions
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Amazon to Rent Out Supercomputers
- Amazon Émigré Starts Network Monitoring Firm
- HP’s Putting a Back Door in the Itanium Alamo
- Linux Virtualization and Tired Open Source Myths
- CloudLinux Announces Preferred Partner Program
- MapR Pushes the Hadoop Envelope
- Rightware Announces Gaming Performance Benchmark for OpenGL ES 3.0/Halti
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- 3Dconnexion Announces its Newest 3D Mouse - the SpaceMouse Pro
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- A Closer Look at Damn Small Linux
- Linus' Top Ten SCO Barbs
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .



















