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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
.. Objectives
.. ----------
.. At the end of this tutorial, you will be able to:
.. 1. Prepare scripts using 'Control Operators'.
.. 2. Understand what 'Environment Variables' are.
.. Prerequisites
.. -------------
.. 1. Shell scripts & Variables
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello friends and Welcome to the tutorial on
'Control structures and Operators'.
.. L2
{{{ Show the 'Objectives' slide }}}
.. R2
At the end of this tutorial, you will be able to,
1. Prepare scripts using 'Control Operators'.
2. Use 'Environment Variables'.
.. L3
{{{ Switch to the pre-requisite slide }}}
.. R3
Before beginning this tutorial,we would suggest you to complete the
previous tutorials as being displayed currently.
.. L4
{{{ Show slide, 'Control Structures' }}}
.. R4
We have many 'Control Structures and Operators' available in the linux bash.
Let us look at how to use them.
To write an 'if', or an 'if-else' construct, we need to check or test for a
condition(s). The ``test`` command allows us to test for condition(s). It has
a whole range of tests that can be performed. The man page of ``test``
gives you the complete listing of various types of tests that can be performed
with it.
Let's write a simple script with an ``if`` condition that tests whether a
directory with a particular name, exists or not.
.. L5
{{{ Show slide, 'if' }}}
.. R5
Let us create a script named ``dir-test.sh`` with this code.
::
#!/bin/bash
if test -d $1
then
echo "Yes, the directory" $1 "is present"
fi
When the script is run with an argument, it will print a message, if a
directory with the said name exists in the current working directory.
.. R6
Let's write a simple script which returns back whether the argument passed
is negative or not.
.. L6
{{{ Open the file sign.sh and show }}}
::
#!/bin/bash
if test $1 -lt 0
then
echo "number is negative"
else
echo "number is non-negative"
fi
.. R7
We can run the file with a set of different inputs and see if it works.
.. L7
{{{ Switch to terminal }}}
::
./sign.sh -11
.. R8
Instead of using the ``test`` command, square brackets may also be used.
.. L8
.. L9
{{{ Show slide, [ ] - alias for test }}}
.. R9
Note that the spacing is important, when using the square brackets.
The left square bracket ( ``[`` ) should be followed by a space and the right
square bracket ( ``]`` ) should be preceded by a space.
Let's create something interesting using the 'if-else' clause. Let's write a
script, that greets the user, based on the time.
.. L10
{{{ Open the file clause.sh and show }}}
{{{ Highlight the required content wherever necessary, while narrating }}}
.. R10
There are a couple of new things in this script. ``$LOGNAME`` is another
'environment variable', which has the login name of the user. The variables,
``hour`` and ``now`` are actually taking the output of the commands that
are placed in the back quotes.
Now, let us see how to run loops in bash. We shall look at the ``for`` and
the ``while`` loops.
.. L11
{{{ Show slide 'Exercise' }}}
.. R11
Suppose we have a set of files, whose file-names contain numbers before the
text, say ``08 - Society.mp3``. We would like to rename these files by
removing the numbers before the text. How would we go about doing that?
It is clear from the problem statement that we could loop over the list of
files and rename each of them.
.. R12
First, let us look at a simple ``for`` loop, to understand how it works.
.. L12
{{{ Switch to terminal }}}
::
for animal in rat cat dog man
do
echo $animal
done
.. R13
We just wrote a list of animals, each name separated by a space
and then printed each name on a separate line. The variable ``animal`` is a
'dummy' or a 'loop variable'. It can then be used to refer to the element of
the list that is currently being dealt with. We could, obviously, use
something as lame as ``i`` in place of ``animal``.
.. L13
.. R14
To generate a range of numbers and iterate over them, we do the following.
.. L14
{{{ Open the script ``for-1.sh`` and show }}}
.. R15
Now, let us run the script and see what we get,
.. L15
{{{ Switch to terminal }}}
::
sh for-1.sh
.. R16
Now, we use a ``for`` loop to list the files that we are interested in.
.. L16
{{{ Open the script ``for-2.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh for-2.sh
.. R17
If the file-names contain spaces, ``for`` assumes, each word separated by a
space,to be a single item in the list and prints it in a separate line. We
could modify the script slightly to overcome this problem.
.. L17
{{{ Open the script ``for-3.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh for-3.sh
.. R18
Now, we have each file name printed on a separate line. The file names are
in the form ``dd - Name.mp3`` and it has to be changed to the format
``Name.mp3``. Also, if the name has spaces, we wish to replace it with
hyphens.
.. L18
{{{ Open the script ``for-4.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh for-4.sh
.. R19
Now, we simply replace the echo command with a ``mv`` command.
.. L19
{{{ Open the script ``for-5.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh for-5.sh
.. R20
We see that we get our required output. All the files have been renamed and
the spaces are removed.
Now let us move ahead with ``while`` loop.
The ``while`` command allows us to continuously execute a block of commands
until the command that is controlling the loop is executing successfully.
.. L20
.. R21
Let's start with the lamest example of a ''while'' loop.
.. L21
{{{ Open the script ``while-1.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh while-1.sh
.. R22
This, as you can see, is an infinite loop that prints ``True``.
Say, we wish to write a simple program that takes input from the user
and prints it back, until the input is ``quit``, which then quits the program.
.. L22
{{{ Open the script ``while-2.sh`` and show }}}
{{{ Switch to terminal }}}
::
sh while-2.sh
.. L23
{{{ Show slide, Environment Variables vs. Environment Variables }}}
.. R23
'Environment variables' are a way of passing information from the shell to the
programs that are run in it. Standard UNIX variables are split into two
categories,'Environment variables' and 'Shell variables'. In broad terms,
'Shell variables' apply only to the current instance of the shell and are
used to set short-term working conditions; 'Environment variables' have a
farther reaching significance, and are set at login, valid for the duration of
the session. By convention, 'Environment variables' have UPPER CASE and 'Shell
variables' have lower case names.
.. R24
To see all the variables and their values, we could use any of the
following,
.. L24
{{{ Switch to terminal }}}
::
printenv | less
env
.. R25
We have looked at the 'PATH' variable, in the previous tutorial. We shall now
use the ``export`` command to change it's value.
.. L25
::
export PATH=$PATH:$HOME/bin
.. R26
Observe the difference in the value of 'PATH' variable before and after
modifying it.
``export`` command is used to export a variable to the environment of all
the processes that are started from that shell.
.. L26
.. L27
{{{ Switch to 'Summary' slide }}}
.. R27
This brings us to the end of this tutorial.
In this tutorial, we have learnt to,
1. Prepare scripts using control structures like ``if``, ``if-else``,
``for`` and ``while``.
2. Use 'environment variables'.
3. Export a variable to the environment of all the processes, using
the ``export`` command.
.. L28
{{{ Show self assessment questions slide }}}
.. R28
Here are some self assessment questions for you to solve:
1. Print the text ``dog man`` in such a way that the prompt continues after
the text.
2. How can you add a new path variable ``/myscripts`` to $PATH variable ?
.. L29
{{{ Solutions of self assessment questions on slide }}}
.. R29
And the answers,
1. We print the given text using the ``echo`` command by using an additional
option -n as,
::
$echo -n dog man
2. We can add a new path variable by using the export command as,
::
$export PATH=$PATH://myscripts
.. L30
{{{ Show the SDES & FOSSEE slide }}}
.. R30
Software Development techniques for Engineers and Scientists - SDES, is an
initiative by FOSSEE. For more information, please visit the given link.
Free and Open-source Software for Science and Engineering Education - FOSSEE, is
based at IIT Bombay which is funded by MHRD as part of National Mission on
Education through ICT.
.. L31
{{{ Show the ``About the Spoken Tutorial Project'' slide }}}
.. R31
Watch the video available at the following link. It summarises the Spoken
Tutorial project.If you do not have good bandwidth, you can download and
watch it.
.. L32
{{{ Show the `` Spoken Tutorial Workshops'' slide }}}
.. R32
The Spoken Tutorial Project Team conducts workshops using spoken tutorials,
gives certificates to those who pass an online test.
For more details, contact contact@spoken-tutorial.org
.. L33
{{{ Show the ``Acknowledgements'' slide }}}
.. R33
Spoken Tutorial Project is a part of the "Talk to a Teacher" project.
It is supported by the National Mission on Education through ICT, MHRD,
Government of India. More information on this mission is available at the
given link.
.. L34
{{{ Show the Thank you slide }}}
.. R34
Hope you have enjoyed this tutorial and found it useful.
Thank you!
|