I/O Redirection, The Pipe Facility and Background Processing
PAGEFLOP
FORWARD
SELAGAIN
WRONGSEL
YOUR SELECTION IS NOT IN THE 0 - #SELIMIT RANGE Please Try Again
SELRTRN
NEXT
$V1$
$V1$
AANVANG
Copyright CourseWare Technologies Inc., 1985-88
Lesson - 7
I/O REDIRECTION, THE PIPE FACILITY
AND BACKGROUND PROCESSING
MENU
I/O-Pipes-Backgrounding|Topics to Learn|7-0|11,44
# Topic
--- -------
1 - Standard Input, Standard Output
2 - Standard Input Redirection
3 - Standard Output Redirection
4 - File Input/Output Redirection
5 - First Lesson Review
6 - The UNIX Pipe Concept
7 - Background Processing
8 - Second Lesson Review
0 - Return to the Main Menu
TOPIC1
Standard Input, Standard Output
P3
I/O-Pipes-Backgrounding|Standard Input, Standard Output|7-1.1|3,54
The standard input (stdin) is the source from
which a command expects information or data. Normally
a command takes its input from the keyboard connected
to your terminal.
P4
I/O-Pipes-Backgrounding|Standard Input, Standard Output|7-1.2|3,54
The standard output (stdout) is the place that
commands send their output by default. Normally,
output is sent to your terminal screen.
TOPIC2
Standard Input Redirection
P5
I/O-Pipes-Backgrounding|Standard Input Redirection|7-2.1|9,56
You can redirect the standard input. Redirecting
instructs the system to look in a file for input instead
of your keyboard.
The command will process the contents of the file
exactly as it would if you had typed the data on your
keyboard. This avoids the need to retype data each time
you want it processed.
P6
I/O-Pipes-Backgrounding|Standard Input Redirection|7-2.2|7,46
The < symbol is used to redirect the
standard input. For example, the command:
prog.run < data.in
tells the command prog.run to take its input from
the file data.in instead of from the keyboard.
P7
I/O-Pipes-Backgrounding|Standard Input Redirection|7-2.3|9,54
Note that the file that a command takes its input
from may contain commands instead of data. For example
look at the file commands:
$ cat commands
ls
who
ps
$
TOPIC3
Standard Output Redirection
P8
I/O-Pipes-Backgrounding|Standard Output Redirection|7-3.1|5,54
You can also redirect the standard output.
This instructs the system to place the output of a
command somewhere other than on your terminal screen.
You can redirect the output of a command to a file, a
floppy disk, a magnetic tape, a printer, etc.
P9
I/O-Pipes-Backgrounding|Standard Output Redirection|7-3.2|7,50
The > symbol is used to redirect output
from the terminal to a file. For example:
ls > directory
redirects a listing of the current directory into
a text file called directory.
P10
I/O-Pipes-Backgrounding|Standard Output Redirection|7-3.3|8,54
You can also append the output of a command to
the end of an existing file by using the >> symbol.
For example:
who >> user_log
appends the current user list to the end of the existing
file user_log.
TOPIC4
File Input/Output Redirection
P11
I/O-Pipes-Backgrounding|File Input/Output Redirection|7-4.1|8,47
Both input and output may be redirected
simultaneously. For example the command:
myprog < data_in > results
tells the program myprog to take its input from
the file datain and send the output to a file
called results.
P12
I/O-Pipes-Backgrounding|File Input/Output Redirection|7-4.2|3,54
Whenever possible, the system redirects any
error messages, called the standard error to the
standard output.
P13
I/O-Pipes-Backgrounding|File Input/Output Redirection|7-4.3|13,54
The standard error may also be redirected to a
file. For example, if we wished to redirect both the
standard error and the standard output to the file
results, the command would be:
Under the UNIX shell:
myprog < data_in 1 > results 2 >& 1
Under the C shell:
myprog < data_in >& results
NOTE: 2 > filename, redirects the standard
error only to the file filename.
TOPIC5
First Lesson Review
TOPIC6
The UNIX Pipe Concept
P14
I/O-Pipes-Backgrounding|The UNIX Pipe Concept|7-6.1|4,54
A pipe provides the user with a means of
connecting the output of one command to the input
of another, i.e., what comes out of one process is
"piped" into another.
P15
I/O-Pipes-Backgrounding|The UNIX Pipe Concept|7-6.2|9,54
The pipe process is signified by the shell
character | . For example:
cat report | wc
will send the output of cat report, that is the file
report to the wc (for w ord count) program. This
program will count the number of words in the file
report.
P16
I/O-Pipes-Backgrounding|The UNIX Pipe Concept|7-6.3|11,54
Some Examples of Commands Using Pipes:
--------------------------------------
who | wc -l
Counts and prints the number of users on the system.
tbl chapter_6 | nroff | col
Produces formatted results of the data in file
chapter_6 in tabular form without reverse line feeds.
TOPIC7
Background Processing
P17
I/O-Pipes-Backgrounding|Background Processing|7-7.1|10,54
UNIX is a multiprocessing system; therefore,
it permits one to execute more than one program or
process at a time in a time-sharing fashion.
When a program runs from your terminal "while you
wait," and doesn't allow you to use your terminal again
until it is finished and gives you your prompt back,
the program is said to run in the foreground and is
called a foreground process.
P18
I/O-Pipes-Backgrounding|Background Processing|7-7.2|6,54
You can also run a program in the background by
appending an ampersand, & , to the command line. You
don't have to wait for a background process to finish
before giving the Shell additional commands. You get
your prompt back as soon as you press <CR> and the
backgrounded command runs without your attention.
P19
I/O-Pipes-Backgrounding|Background Processing|7-7.3|13,54
Let's look at an example of a background command.
When the following command is issued,
fixit < chapter_1 > result_1 &
the system will respond with a process identification
number for the command, followed by the shell prompt.
The fixit command will be still running in the
background.
NOTE: While many processes may be run in the
background, only one process may run in foreground.
P20
I/O-Pipes-Backgrounding|Background Processing|7-7.4|13,58
Since text formatting with programs like nroff
is very time-consuming, they are good tasks to run in
the background. For example,
tbl report | nroff -mm -o15- > report_nroffed &
will run 2 processes, tbl and nroff in the background.
The process id numbers will be printed on your screen
before you get your prompt back, i.e.,
$ tbl report | nroff -mm -o15- > report_nroffed &
1843
1844
$
P21
I/O-Pipes-Backgrounding|Background Processing|7-7.5|4,54
To see a list of all of your backgrounded and
stopped processes, you can use either the ps command
or a command called jobs. The jobs command
is quicker but less thorough than ps.
I/O-Pipes-Backgrounding|Background Processing|7-7.5|11,54
Some systems will allow you to temporarily stop
a process, so you can do something else. The process
is placed in a state of suspended animation and you
are returned your shell prompt. The process is in
the background, but is not being executed, it is just
waiting to be returned to the foreground again. To
stop a foreground process, you type
<CONTROL-z>
You will see the word Stopped and your Shell prompt.
P22
I/O-Pipes-Backgrounding|Background Processing|7-7.6|10,54
To return a stopped process or any backgrounded
process to the foreground again, type:
fg
To see a list of all of your backgrounded and
stopped processes, you can use either the ps command
or a command called jobs. The jobs command
is quicker but less thorough than ps.
TOPIC8
Second Lesson Review