Redirecting Input and output

advertisement
Chapter 7
Standard Input and Standard Output
Redirecting Standard Input and Standard Output
Pipes
Filters
tee
Miscellaneous utility commands
Regular Expressions
The Shell - The shell is a utility program. Its purpose is to interface with the user and
the Operating system.
Redirecting Input and output – The terminal and keyboard as files
CPU
Operating System
1 Standard output
Monitor
2 Standard error
Standard
Output and
Standard error
Keyboard
Standard
Input
Shell
Command
(program)
0 Standard Input
program
memory
data
memory
User
When the user enters a command from the keyboard (standard input), the shell interprets
the line entered, if the command (program) is found the command is executed
(processed), and the output , by default, is displayed on the screen using file descriptor 1 (
Standard Output ) and error messages are also displayed on the screen using file
descriptor 2 (Standard Error)
LINUX is device independent, and the default standard output and standard input can be
redirected by the shell.
Redirection
Special symbols and commands are used to tell the shell how to redirect input and output.
>
Redirect Standard Output
command [arguments] > filename
<
Redirect Standard Input
command [arguments] < filename
>>
Redirect Standard Output and append to a file
|
(pipe) The shell takes output from one command and sends it as input to another
command. For example, the following command will redirect the output from
the command cat to the command sort. The output from sort will be redirected
and stored in a file sorted.file.
cat file_name | sort >sorted.file
tee
The tee command sends output into two directions. The tee command
sends output to standard output ( if not redirected) and to a file.
to file
to standard output
Examples:
who | tee who.out
output goes to standard output and file who.out
who | tee who.out | grep root  output from tee goes to file who.out and
standard output is redirected to the command grep
Types of Files
Ordinary Files
- text files, program files (executable), word processing files , schell scripts ..
Character Special Files
- produce stram of bytes ( raw data ) – these files are related to serial input/output
devices
Block special files
- process their data a block at a time ( used for reading and writing to disk )
Ordinary Files can be structured in various ways
An ASCII file is a file containing a sequence of ASCII characters (bytes)
The cat command can combine files
For example, the following command combines two files and by instructing the
shell to redirect the output to a new file, the shell creates the new file and puts the
combined data into the new file by appending file2 to file1.
$ cat file1 file2 > combinedfile<enter>
The cut and paste command
The cut command allows you to retrieve some fields in a file
and the paste command allows you to combine files
Exercise 7-7 Using cut and paste
Filters and using Pipes
Command that take input – can modify their input – and produce output
are referred to as filters.
Example:
The sort command
$ cat file1 | sort > sorted_file<enter>
In the above example:
Step1. Using the cat command the data in file1
is sent to sort using a pipe command.
Step2. The sort command receives the data through the pipe
And sorts the data
Step3. The sehll is then instructed by the ouput redirection command >
to create a new file sorted_file that will contain the sorted data.
The content of file1 does not change. Thus, is a filter.
Activity 7-9 Using sort and Activity 7-11 using pipes
Introductin grep, egrep and fgrep
grep – (global regular expression)
The grep utility command can take a string of characters ( string pattern )
Search through a file – line by line – and if a match is found that line is
displayed to standard output unless it is re-directed.
The pattern itself is referred to as a regular expression.
A regular expression can match more than one string.
Coammnd Format:
grep [options] pattern [file…]
Some common regular expression characters that can be used to define a
matching pattern that matches more than one string are:
*
matches all characters
?
matches a single character
[]
matches a set or range of characters
ex: [abc]  matches either a b or c
ex: [a-z]  matches the range of characters a –z
^abc matches the string abc at the beginning of the line
$abc matches the tring abc at the end of a line
Extended regular expressions include many more special characters
including the following :
+
one ore more occurrences of the preceding regular expression
ex: ab+c
 pattern starts with ab and is followed by one or
more b’x ending in c
?
0 or 1 occurrence of preceding regular expression
.
0 or more occurrences of preceding regular expression
|
alternate ( match either or ) ex: ‘ab|cd’
Activity 7-13
Using the pr command
The pr command is used to format output – It has some 20 options
producing different results
The output can be piped to lpr and the formatted output is then printed
Activity: 7-15
The tee command
tee
The tee command sends output into two directions. The tee command
sends output to standard output ( if not redirected) and to a file.
to file
to standard output
Examples:
who | tee who.out
output goes to standard output and file who.out
who | tee who.out | grep root  output from tee goes to file who.out and
standard output is redirected to the command grep
tr command
the tr command format:
tr [options] string1 [options] string]
Other utilities
Ispell , head, tail
Uniq, diff, and comm., gzip, gunzip, zcat, and tar
Download