-
Notifications
You must be signed in to change notification settings - Fork 485
/
unix3.html
184 lines (158 loc) · 7.2 KB
/
unix3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> UNIX Tutorial Three </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Copyright" content="Michael Stonebank, 1995-2003" />
<link href="unixtut2.css" rel="stylesheet" type="text/css" />
<link href="unixtut2-print.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body>
<div id="container">
<h1>UNIX Tutorial Three </h1>
<h2>3.1 Redirection </h2>
<p>Most processes initiated by UNIX commands write to the standard output (that
is, they write to the terminal screen), and many take their input from the standard
input (that is, they read it from the keyboard). There is also the standard
error, where processes write their error messages, by default, to the terminal
screen. </p>
<p> We have already seen one use of the <span class="command">cat</span> command to write the
contents of a file to the screen. </p>
<p> Now type <span class="command">cat</span> without specifing a file to read </p>
<p class="cli"> % cat</p>
<p>Then type a few words on the keyboard and press the [<b>Return</b>] key. </p>
<p> Finally hold the [<b>Ctrl</b>] key down and press [<b>d</b>] (written
as <b>^D</b> for short) to end the input. </p>
<p> What has happened? </p>
<p> If you run the <span class="command">cat</span> command without specifing a file to read,
it reads the standard input (the keyboard), and on receiving the 'end of file'
(<b>^D</b>), copies it to the standard output (the screen). </p>
<p> In UNIX, we can redirect both the input and the output of commands. </p>
<p> </p>
<h2>3.2 Redirecting the Output </h2>
<p>We use the > symbol to redirect the output of a command. For example, to
create a file called <strong>list1</strong> containing a list of fruit, type
</p>
<p class="cli">% cat > list1</p>
<p>Then type in the names of some fruit. Press [<b>Return</b>] after each
one. </p>
<p class="cli">pear<br>
banana<br>
apple<br>
^D {this means press [Ctrl] and [d] to stop}</p>
<p>What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called <strong>list1</strong> </p>
<p> To read the contents of the file, type </p>
<p class="cli">% cat list1</p>
<h3>Exercise 3a</h3>
<p>Using the above method, create another file called <strong>list2</strong> containing
the following fruit: orange, plum, mango, grapefruit. Read the contents of <strong>list2</strong></p>
<p> </p>
<h3>3.2.1 Appending to a file </h3>
<p>The form >> appends standard output to a file. So to add more items to
the file <strong>list1</strong>, type </p>
<p class="cli"> % cat >> list1</p>
<p>Then type in the names of more fruit </p>
<p class="cli"> peach<br />
grape<br />
orange<br />
^D (Control D to stop)</p>
<p>To read the contents of the file, type </p>
<p class="cli"> % cat list1</p>
<p>You should now have two files. One contains six fruit, the other contains four
fruit. </p>
<p>We will now use the cat command to join (concatenate) <strong>list1</strong> and <strong>list2</strong> into a new file called <strong>biglist</strong>.
Type </p>
<p class="cli"> % cat list1 list2 > biglist</p>
<p>What this is doing is reading the contents of <strong>list1</strong> and <strong>list2</strong>
in turn, then outputing the text to the file <strong>biglist</strong> </p>
<p> To read the contents of the new file, type </p>
<p class="cli"> % cat biglist</p>
<h2>3.3 Redirecting the Input </h2>
<p>We use the < symbol to redirect the input of a command. </p>
<p> The command sort alphabetically or numerically sorts a list.
Type </p>
<p class="cli"> % sort </p>
<p> Then type in the names of some animals. Press [Return] after
each one. </p>
<p class="cli">dog<br />
cat<br />
bird<br />
ape<br />
^D (control d to stop)</p>
<p> The output will be </p>
<p class="output"> ape<br />
bird <br />
cat <br />
dog </p>
<p> Using < you can redirect the input to come from a file rather than the
keyboard. For example, to sort the list of fruit, type </p>
<p class="cli"> % sort < biglist </p>
<p> and the sorted list will be output to the screen. </p>
<p> To output the sorted list to a file, type, </p>
<p class="cli"> % sort < biglist > slist </p>
<p> Use cat to read the contents of the file <strong>slist</strong></p>
<h2>3.4 Pipes</h2>
<p>To see who is on the system with you, type </p>
<p class="cli"> % who </p>
<p>One method to get a sorted list of names is to type, </p>
<p class="cli"> % who > names.txt<br />
% sort < names.txt </p>
<p>This is a bit slow and you have to remember to remove the temporary file called
names when you have finished. What you really want to do is connect the output
of the who command directly to the input of the sort
command. This is exactly what pipes do. The symbol for a pipe is the vertical
bar | </p>
<p> For example, typing </p>
<p class="cli"> % who | sort </p>
<p>will give the same result as above, but quicker and cleaner. </p>
<p> To find out how many users are logged on, type </p>
<p class="cli"> % who | wc -l</p>
<h3>Exercise 3b</h3>
<p>Using pipes, display all lines of <strong>list1</strong> and <strong>list2</strong>
containing the letter 'p', and sort the result.</p>
<p><a href="pipeanswer.html">Answer available here</a></p>
<h2>Summary </h2>
<table border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<th>Command</th>
<th>Meaning</th>
</tr>
<tr>
<td class="command"><var>command</var> > <var>file</var></td>
<td>redirect standard output to a file</td>
</tr>
<tr>
<td class="command"><var>command</var> >> <var>file</var></td>
<td>append standard output to a file </td>
</tr>
<tr>
<td class="command"><var>command</var> < <var>file</var></td>
<td>redirect standard input from a file</td>
</tr>
<tr>
<td class="command"><var>command1</var> | <var>command2</var></td>
<td>pipe the output of command1 to the input of command2</td>
</tr>
<tr>
<td class="command">cat <var>file1 file2</var> > <var>file0</var></td>
<td>concatenate file1 and file2 to file0</td>
</tr>
<tr>
<td class="command">sort</td>
<td>sort data</td>
</tr>
<tr>
<td class="command">who</td>
<td>list users currently logged in</td>
</tr>
</table>
<p align="center" class="navbar"><a href="unix2.html"><img src="media/left.gif" alt="Previous" width="37" height="39" border="0" /></a>
<a href="index.html"><img src="media/home.gif" alt="Home" width="81" height="39" border="0" /></a><a href="unix4.html"><img src="media/right.gif" alt="Next" width="37" height="39" border="0" /></a></p>
<p align="center">
<iframe src="http://rcm.amazon.com/e/cm?t=unixtutorialf-20&o=1&p=48&l=ur1&category=software&banner=1YTTZDQHSHTZRF996982&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
</p>
<p class="date"> M.Stonebank@surrey.ac.uk, © 9th October 2000 </p>
</div>
</body>
</html>