There are times when you’d like to save the output from a command instead of just having it displayed on the monitor. The bash shell provides a few different operators that allow you to redirect the output of a command to an alternative location (such as a file). Redirection can be used for input as well as output, redirecting a file to a command for input. This section describes what you need to use redirection in your shell scripts.
The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol for this:
Anything that would appear on the monitor from the command instead is stored in the output
The redirect operator created the file test6 (using the default umask settings) and redirected the output from the date command to the test6 file. If the output file already exists, the redirect operator overwrites the existing file with the new file data:$ who > test6
Now the contents of the test6 file contain the output from the who command.
Sometimes, instead of overwriting the file’s contents, you may need to append output from a command to an existing file, for example if you’re creating a log file to document an action on the system. In this situation, you can use the double greater-than symbol (>>) to append data:
$ date test6
$ cat test6
rich pts/0 Sep 24 17:55
Tue Sep 24 18:02:14 EDT 2007
The test6 file still contains the original data from the who command processed earlier, plus now it contains the new output from the date command.
Input redirection is the opposite of output redirection. Instead of taking the output of a command and redirecting it to a file, input redirection takes the content of a file and redirects it to a command.
The input redirection symbol is the less-than symbol (<):
The easy way to remember this is that the command is always listed first in the command line, and the redirection symbol ‘‘points’’ to the way the data is flowing. The less-than symbol indicates that the data is flowing from the input file to the command.
Here’s an example of using input redirection with the wc command:$ wctest6
The wc command provides a count of text in the data. By default it produces three values:
By redirecting a text file to the wc command, you can get a quick count of the lines, words, and bytes in the file. The example shows that there are 2 lines, 11 words, and 60 bytes in the test6 file.
There’s another method of input redirection, called inline input redirection. This method allows you to specify the data for input redirection on the command line instead of in a file. This may seem somewhat odd at first, but there are a few applications for this process.
The inline input redirection symbol is the double less-than symbol . Besides this symbol, you must specify a text marker that delineates the beginning and end of the data used for input. You can use any string value for the text marker, but it must be the same at the beginning of the data and the end of the data:command marker
When using inline input redirection on the command line, the shell will prompt for data using the secondary prompt, defined in the PS2 environment variable. Here’s how this looks when you use it:$ wc << EOF
The secondary prompt continues to prompt for more data until you enter the text marker. Thewc command performs the line, word, and byte counts of the data supplied by the inline input redirection.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.