| By Dave Taylor | Article Rating: |
|
| July 27, 2004 12:00 AM EDT | Reads: |
11,502 |
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 11,502
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 "<". |
||||
- Kindle 2 vs Nook
- Is Cloud Computing Like Teenage Sex?
- GovIT Expo Highlights Cloud Computing
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Cloud Computing Can Revitalize Your Career as Software Developer
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Yahoo! SVP Shelton Shugar to Discuss Innovation at Cloud Computing Expo
- Virtualization Journal "Readers' Choice Awards" Voting Is Now Open
- Einstein, Sharks and Clouds: IT Security in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Virtualization Expo Call for Papers Deadline December 15
- Amazon Web Services Database in the Cloud
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Is Cloud Computing Like Teenage Sex?
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Ulitzer News: Search vs New Media
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Confessions of a Ulitzer Addict
- GovIT Expo Highlights Cloud Computing
- Twitter, Linked In, Ning and Ulitzer: Easy Personal Branding Strategy
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- 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
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Where Are RIA Technologies Headed in 2008?
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- i-Technology Viewpoint: The New Paradigm of IT Buying
- Is Linux Desktop-Ready Yet...or Not?































