-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises 2
417 lines (208 loc) · 8.86 KB
/
exercises 2
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
2. Basic Tasks
Everybody should be able to complete the following tasks.
2.1. Parameters/Variables
The following tasks concern primarily the use of parameters/variables.
2.1.1. Counting
The Linux utility seq are useful for generating sequences of numbers. Here, you will write your own approximation to these utilities. Write a pure shell script seq.sh for counting.
Your script must accept and require exactly two integer arguments:
the value at which to start counting
the value at which to stop counting
Output must be one number per line.
You may assume:
that exactly two arguments are indeed provided,
that the arguments provided are indeed integers, and
that the stop value is greater than or equal to the start value.
Here are some examples:
$ sh seq.sh 1 4
1
2
3
4
$
$ sh seq.sh 98 101
98
99
100
101
Tip
You will need to use:
a while loop
the -lt operator of the test command (man test)
arithmetic expansion
2.1.2.Checking Arguments
Extend your script seq.sh such that it verifies that exactly and only two arguments have in fact been provided.
Examples:
$ sh seq.sh
usage: seq.sh START STOP
$ sh seq.sh 100
usage: seq.sh START STOP
$ sh seq.sh 98 100
98
99
100
$ sh seq.sh 98 100 4
usage: seq.sh START STOP
Tip
The special variable $# contains a count of the number of arguments provided.
Read the manual page for test to see how you can compare values.
Your script should yield an appropriate exit code.
2.1.3. Default Arguments
Extend your script seq.sh such that:
if no second argument is provided, then a default start value of 1 is used
Example:
$ sh seq.sh 4
1
2
3
4
Tip
The form ${parameter:-word} can be used to use default values. The form ${parameter:=word} can be used to assign default values.
2.1.4. More Default Arguments
Extend your script seq.sh such that:
if no arguments are provided, then it counts from 1 upwards forever (or, at least until stopped by some other means).
Example:
$ sh seq.sh | head -6
1
2
3
4
6
Tip
Look carefully at and extend the condition of your while loop.
2.2. Command Substitution
The following tasks concern primarily the use of command substitution.
2.2.1. Preliminaries
The du -s command outputs a summary of the disk space occupied by a file or directory (du stands for disk usage, s stands for summary).
Examples:
$ du -s ~/.zshenv
4 /home/charlie/.zshenv
$ du -s ~/local/sbenv
65288 /home/charlie/local/sbenv
The cut utility selects individual or groups of fields from its input.
Examples:
$ du -s ~/.zshenv | cut -f 1
4
$ du -s ~/local/sbenv | cut -f 2
/home/charlie/local/sbenv
Tip
For the details of what these utilities do, read their manual pages.
Many utilities accept flags which adjust their behaviour. If you need a particular behaviour from a utility, it's likely that others have needed that behaviour too, so its likely that a relevant flag exists.
2.2.2. File/Directory Sizes, From Oldest to Newest
Write a script sizes.sh which outputs one line for each file or directory in the current working directory:
each line must contain
the size of the file/directory contents in kilobytes
the name of the file or directory
lines must be ordered by their last-modified date, from oldest to youngest
Example:
$ sh sizes.sh
4 Makefile
12 labsheet-01-intro.ascii
36 labsheet-01-intro.html
20 01-intro.slide
168 01-intro.html
192 02-more.html
28 02-more.slide
40 labsheet-02-more.html
12 labsheet-02-more.ascii
(These files are in order of last modified date).
Tip
You may choose to use command substitution.
The command ls -tr outputs a list of files/directories in the required order.
Does your solution require a while loop? A solution does not requires a while loop.
2.2.3. File Size Report
Write a scipt file_size_report.sh which:
accepts some number of file names as arguments, and
for each file, outputs a message:
The size of FILENAME is SIZE kilobytes
(where FILENAME and SIZE are replaced appropriately.
Example:
$ sh file_size_report.sh ~/.zshenv ~/local/sbenv
The size of /home/charlie/.zshenv is 4 kilobytes
The size of /home/charlie/local/sbenv is 65288 kilobytes
Tip
You may choose to use command substitution, and may have to use a pipe within the command being substituted.
2.3. Shell Functions
The following tasks concern primarily the use of shell functions.
2.3.1. Web Cat
Write a shell function wcat which accepts some number of URLs as arguments and downloads each, outputting the downloaded content to standard output.
Example:
$ wcat http://www.google.com/ http://www.facebook.com/
.
<copious output>
Tip
Use the wget utility. You'll have to read its manual page. There is a wget option to direct output to standard output.
2.3.2. Repeat
Write a shell function called repeat which accepts a command and (optionally) some arguments and executes the command with its arguments (if any) once per second.
$ repeat date
Thu 10 Nov 2011 10:34:31 GMT
Thu 10 Nov 2011 10:34:32 GMT
Thu 10 Nov 2011 10:34:33 GMT
Thu 10 Nov 2011 10:34:34 GMT
...
$ repeat du -s ~
968238 /home/charlie
968129 /home/charlie
967846 /home/charlie
967732 /home/charlie
...
Tip
The command sleep can be used to sleep for some number of seconds.
2.3.3. Find a Usable Executable
Write a shell function usable which accepts some number of arguments and outputs the full path of the executable of the first for which the command is in fact installed on the system at hand.
Example:
$ usable firefox opera uzbl
/usr/local/bin/opera
$ usable urxvt rxvt xterm
/usr/local/bin/xterm
Here, no command named firefox is installed, but opera is. urxvt and rxvt are not installed, but xterm is.
Tip
The external command 'which' should be used to locate executables. Use command 'which XXX' to be sure that you are indeed using the external which command.
Your function should succeed if a suitable executable is found, and fail otherwise.
3. Advanced Tasks
Some of these tasks are significantly more difficult, and may involve material that has not been covered in the course.
3.1. Capture Output
The rationale for this exercise will become apparent in the context of the [chronix] task, below.
Write a script capture:
capture command [ arguments ... ]
capture
When called with a command and (optional) arguments, capture:
calls the command with its arguments, capturing the command's standard output and standard error in a temporary file, and
outputs to standard output only the name of the temporary file.
Its exit code should be the same as that of the command.
For example:
#!/bin/sh
capture ls /etc # outputs only "/tmp/_capture_charlie_37176.Hs6dqi"
# $? is '0' here
capture ls /non-existent # outputs only "/tmp/_capture_charlie_37269.k2s8AC"
# $? is '1' here
In this case, /tmp/_capture_charlie_37176.Hs6dqi contains the output of the first command, and /tmp/_capture_charlie_37269.k2s8AC contains the output of the second command.
The temporary file should be created in the directory indicated by the environment variable TMPDIR, if it exists and is not null, or in the directory /tmp, otherwise.
When called with no arguments, capture should output a (possibly empty) list of previously-created temporary files that have not yet been removed.
For example:
#!/bin/sh
capture | xargs rm
could be used to remove any remaining temporary files.
Tip
The mktemp utility may prove useful (man mktemp).
3.2. Chronix
The cron daemon can be used to run jobs at scheduled intervals.
If a cron job produces any output (on either standard output or standard error), then that output is emailed to the owner of the job.
This creates a problem.
Frequently, cron jobs do produce output, although that output is of little interest.
Frustrated by the number of unnecessary emails they receive, users add flags to commands or redirect their outputs to suppress these unnecessary emails.
This, of course, is then a problem when from time to time the output of a cron jobs is actually needed.
Write a script chronix:
chronix command [ arguments ... ]
chronix calls the given command with its arguments, if any, and behaves as follows:
If the command's exit code is 0, then chronix discards all of the commands outputs and exits itself with an exit code of 0.
If the command's exit code is non-zero, then chronix:
outputs to its own standard output all of the output which the command produced, and
exits with the same exit code as that of the command.
For example, chronix might be used in a crontab file as follows:
@daily chronix sh backup-script.sh
Here, the backup script may or may not produce output. If it succeeds, any output will be discarded. If it fails, its output will be emailed to the user.
Tip
The [capture] script described above may prove useful.
chronix should remove any temporary files it creates.
Note
This exercise is based on the chronic command which is part of the moreutils suite of utilities.